mac: Let IPhotoDataProvider::GetAlbumNames() return albums in a deterministic order.
[chromium-blink-merge.git] / base / test / launcher / test_results_tracker.h
blob53a4e560f8ac5cef8c600fa03230e74929d5d2e6
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
6 #define BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/test/launcher/test_result.h"
15 #include "base/threading/thread_checker.h"
17 namespace base {
19 class CommandLine;
20 class FilePath;
22 // A helper class to output results.
23 // Note: as currently XML is the only supported format by gtest, we don't
24 // check output format (e.g. "xml:" prefix) here and output an XML file
25 // unconditionally.
26 // Note: we don't output per-test-case or total summary info like
27 // total failed_test_count, disabled_test_count, elapsed_time and so on.
28 // Only each test (testcase element in the XML) will have the correct
29 // failed/disabled/elapsed_time information. Each test won't include
30 // detailed failure messages either.
31 class TestResultsTracker {
32 public:
33 TestResultsTracker();
34 ~TestResultsTracker();
36 // Initialize the result tracker. Must be called exactly once before
37 // calling any other methods. Returns true on success.
38 bool Init(const CommandLine& command_line) WARN_UNUSED_RESULT;
40 // Called when a test iteration is starting.
41 void OnTestIterationStarting();
43 // Adds |test_name| to the set of discovered tests (this includes all tests
44 // present in the executable, not necessarily run).
45 void AddTest(const std::string& test_name, const std::string& file, int line);
47 // Adds |test_name| to the set of disabled tests.
48 void AddDisabledTest(const std::string& test_name);
50 // Adds |result| to the stored test results.
51 void AddTestResult(const TestResult& result);
53 // Prints a summary of current test iteration to stdout.
54 void PrintSummaryOfCurrentIteration() const;
56 // Prints a summary of all test iterations (not just the last one) to stdout.
57 void PrintSummaryOfAllIterations() const;
59 // Adds a string tag to the JSON summary. This is intended to indicate
60 // conditions that affect the entire test run, as opposed to individual tests.
61 void AddGlobalTag(const std::string& tag);
63 // Saves a JSON summary of all test iterations results to |path|. Returns
64 // true on success.
65 bool SaveSummaryAsJSON(const FilePath& path) const WARN_UNUSED_RESULT;
67 // Map where keys are test result statuses, and values are sets of tests
68 // which finished with that status.
69 typedef std::map<TestResult::Status, std::set<std::string> > TestStatusMap;
71 // Returns a test status map (see above) for current test iteration.
72 TestStatusMap GetTestStatusMapForCurrentIteration() const;
74 // Returns a test status map (see above) for all test iterations.
75 TestStatusMap GetTestStatusMapForAllIterations() const;
77 private:
78 void GetTestStatusForIteration(int iteration, TestStatusMap* map) const;
80 template<typename InputIterator>
81 void PrintTests(InputIterator first,
82 InputIterator last,
83 const std::string& description) const;
85 struct AggregateTestResult {
86 AggregateTestResult();
87 ~AggregateTestResult();
89 std::vector<TestResult> test_results;
92 struct PerIterationData {
93 PerIterationData();
94 ~PerIterationData();
96 // Aggregate test results grouped by full test name.
97 typedef std::map<std::string, AggregateTestResult> ResultsMap;
98 ResultsMap results;
101 struct CodeLocation {
102 CodeLocation(const std::string& f, int l) : file(f), line(l) {
105 std::string file;
106 int line;
109 ThreadChecker thread_checker_;
111 // Set of global tags, i.e. strings indicating conditions that apply to
112 // the entire test run.
113 std::set<std::string> global_tags_;
115 // Set of all test names discovered in the current executable.
116 std::set<std::string> all_tests_;
118 std::map<std::string, CodeLocation> test_locations_;
120 // Set of all disabled tests in the current executable.
121 std::set<std::string> disabled_tests_;
123 // Store test results for each iteration.
124 std::vector<PerIterationData> per_iteration_data_;
126 // Index of current iteration (starting from 0). -1 before the first
127 // iteration.
128 int iteration_;
130 // File handle of output file (can be NULL if no file).
131 FILE* out_;
133 DISALLOW_COPY_AND_ASSIGN(TestResultsTracker);
136 } // namespace base
138 #endif // BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_