• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/test/launcher/test_result.h"
15 #include "base/threading/thread_checker.h"
16 
17 namespace base {
18 
19 class CommandLine;
20 class FilePath;
21 
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();
35 
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;
39 
40   // Called when a test iteration is starting.
41   void OnTestIterationStarting();
42 
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);
46 
47   // Adds |test_name| to the set of disabled tests.
48   void AddDisabledTest(const std::string& test_name);
49 
50   // Adds |result| to the stored test results.
51   void AddTestResult(const TestResult& result);
52 
53   // Prints a summary of current test iteration to stdout.
54   void PrintSummaryOfCurrentIteration() const;
55 
56   // Prints a summary of all test iterations (not just the last one) to stdout.
57   void PrintSummaryOfAllIterations() const;
58 
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);
62 
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;
66 
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;
70 
71   // Returns a test status map (see above) for current test iteration.
72   TestStatusMap GetTestStatusMapForCurrentIteration() const;
73 
74   // Returns a test status map (see above) for all test iterations.
75   TestStatusMap GetTestStatusMapForAllIterations() const;
76 
77  private:
78   void GetTestStatusForIteration(int iteration, TestStatusMap* map) const;
79 
80   struct AggregateTestResult {
81     AggregateTestResult();
82     ~AggregateTestResult();
83 
84     std::vector<TestResult> test_results;
85   };
86 
87   struct PerIterationData {
88     PerIterationData();
89     ~PerIterationData();
90 
91     // Aggregate test results grouped by full test name.
92     typedef std::map<std::string, AggregateTestResult> ResultsMap;
93     ResultsMap results;
94   };
95 
96   ThreadChecker thread_checker_;
97 
98   // Set of global tags, i.e. strings indicating conditions that apply to
99   // the entire test run.
100   std::set<std::string> global_tags_;
101 
102   // Set of all test names discovered in the current executable.
103   std::set<std::string> all_tests_;
104 
105   // Set of all disabled tests in the current executable.
106   std::set<std::string> disabled_tests_;
107 
108   // Store test results for each iteration.
109   std::vector<PerIterationData> per_iteration_data_;
110 
111   // Index of current iteration (starting from 0). -1 before the first
112   // iteration.
113   int iteration_;
114 
115   // File handle of output file (can be NULL if no file).
116   FILE* out_;
117 
118   DISALLOW_COPY_AND_ASSIGN(TestResultsTracker);
119 };
120 
121 }  // namespace base
122 
123 #endif  // BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
124