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. 4 5 #ifndef BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_ 6 #define BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/callback.h" 14 #include "base/macros.h" 15 #include "base/test/launcher/test_result.h" 16 #include "base/threading/thread_checker.h" 17 18 namespace base { 19 20 class CommandLine; 21 class FilePath; 22 23 // A helper class to output results. 24 // Note: as currently XML is the only supported format by gtest, we don't 25 // check output format (e.g. "xml:" prefix) here and output an XML file 26 // unconditionally. 27 // Note: we don't output per-test-case or total summary info like 28 // total failed_test_count, disabled_test_count, elapsed_time and so on. 29 // Only each test (testcase element in the XML) will have the correct 30 // failed/disabled/elapsed_time information. Each test won't include 31 // detailed failure messages either. 32 class TestResultsTracker { 33 public: 34 TestResultsTracker(); 35 ~TestResultsTracker(); 36 37 // Initialize the result tracker. Must be called exactly once before 38 // calling any other methods. Returns true on success. 39 bool Init(const CommandLine& command_line) WARN_UNUSED_RESULT; 40 41 // Called when a test iteration is starting. 42 void OnTestIterationStarting(); 43 44 // Adds |test_name| to the set of discovered tests (this includes all tests 45 // present in the executable, not necessarily run). 46 void AddTest(const std::string& test_name); 47 48 // Adds |test_name| to the set of disabled tests. 49 void AddDisabledTest(const std::string& test_name); 50 51 // Adds location for the |test_name|. 52 void AddTestLocation(const std::string& test_name, 53 const std::string& file, 54 int line); 55 56 // Adds |result| to the stored test results. 57 void AddTestResult(const TestResult& result); 58 59 // Prints a summary of current test iteration to stdout. 60 void PrintSummaryOfCurrentIteration() const; 61 62 // Prints a summary of all test iterations (not just the last one) to stdout. 63 void PrintSummaryOfAllIterations() const; 64 65 // Adds a string tag to the JSON summary. This is intended to indicate 66 // conditions that affect the entire test run, as opposed to individual tests. 67 void AddGlobalTag(const std::string& tag); 68 69 // Saves a JSON summary of all test iterations results to |path|. Adds 70 // |additional_tags| to the summary (just for this invocation). Returns 71 // true on success. 72 bool SaveSummaryAsJSON( 73 const FilePath& path, 74 const std::vector<std::string>& additional_tags) const WARN_UNUSED_RESULT; 75 76 // Map where keys are test result statuses, and values are sets of tests 77 // which finished with that status. 78 typedef std::map<TestResult::Status, std::set<std::string> > TestStatusMap; 79 80 // Returns a test status map (see above) for current test iteration. 81 TestStatusMap GetTestStatusMapForCurrentIteration() const; 82 83 // Returns a test status map (see above) for all test iterations. 84 TestStatusMap GetTestStatusMapForAllIterations() const; 85 86 private: 87 void GetTestStatusForIteration(int iteration, TestStatusMap* map) const; 88 89 template<typename InputIterator> 90 void PrintTests(InputIterator first, 91 InputIterator last, 92 const std::string& description) const; 93 94 struct AggregateTestResult { 95 AggregateTestResult(); 96 AggregateTestResult(const AggregateTestResult& other); 97 ~AggregateTestResult(); 98 99 std::vector<TestResult> test_results; 100 }; 101 102 struct PerIterationData { 103 PerIterationData(); 104 PerIterationData(const PerIterationData& other); 105 ~PerIterationData(); 106 107 // Aggregate test results grouped by full test name. 108 typedef std::map<std::string, AggregateTestResult> ResultsMap; 109 ResultsMap results; 110 }; 111 112 struct CodeLocation { CodeLocationCodeLocation113 CodeLocation(const std::string& f, int l) : file(f), line(l) { 114 } 115 116 std::string file; 117 int line; 118 }; 119 120 ThreadChecker thread_checker_; 121 122 // Set of global tags, i.e. strings indicating conditions that apply to 123 // the entire test run. 124 std::set<std::string> global_tags_; 125 126 // Set of all test names discovered in the current executable. 127 std::set<std::string> all_tests_; 128 129 std::map<std::string, CodeLocation> test_locations_; 130 131 // Set of all disabled tests in the current executable. 132 std::set<std::string> disabled_tests_; 133 134 // Store test results for each iteration. 135 std::vector<PerIterationData> per_iteration_data_; 136 137 // Index of current iteration (starting from 0). -1 before the first 138 // iteration. 139 int iteration_; 140 141 // File handle of output file (can be NULL if no file). 142 FILE* out_; 143 144 DISALLOW_COPY_AND_ASSIGN(TestResultsTracker); 145 }; 146 147 } // namespace base 148 149 #endif // BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_ 150