test_results_tracker.h 6.2 KB

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