Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: base/test_suite.h

Issue 243088: Add support for flaky tests. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_TEST_SUITE_H_ 5 #ifndef BASE_TEST_SUITE_H_
6 #define BASE_TEST_SUITE_H_ 6 #define BASE_TEST_SUITE_H_
7 7
8 // Defines a basic test suite framework for running gtest based tests. You can 8 // Defines a basic test suite framework for running gtest based tests. You can
9 // instantiate this class in your main function and call its Run method to run 9 // instantiate this class in your main function and call its Run method to run
10 // any gtest based tests that are linked into your executable. 10 // any gtest based tests that are linked into your executable.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 // Prints the exception call stack. 50 // Prints the exception call stack.
51 // This is the unit tests exception filter. 51 // This is the unit tests exception filter.
52 inline long WINAPI UnitTestExceptionFilter(EXCEPTION_POINTERS* info) { 52 inline long WINAPI UnitTestExceptionFilter(EXCEPTION_POINTERS* info) {
53 StackTrace(info).PrintBacktrace(); 53 StackTrace(info).PrintBacktrace();
54 if (g_previous_filter) 54 if (g_previous_filter)
55 return g_previous_filter(info); 55 return g_previous_filter(info);
56 return EXCEPTION_EXECUTE_HANDLER; 56 return EXCEPTION_EXECUTE_HANDLER;
57 } 57 }
58 #endif // OS_WIN 58 #endif // OS_WIN
59 59
60 // Match function used by the GetTestCount method.
61 typedef bool (*TestMatch)(const testing::TestInfo&);
62
60 class TestSuite { 63 class TestSuite {
61 public: 64 public:
62 TestSuite(int argc, char** argv) { 65 TestSuite(int argc, char** argv) {
63 base::EnableTerminationOnHeapCorruption(); 66 base::EnableTerminationOnHeapCorruption();
64 CommandLine::Init(argc, argv); 67 CommandLine::Init(argc, argv);
65 testing::InitGoogleTest(&argc, argv); 68 testing::InitGoogleTest(&argc, argv);
66 #if defined(OS_LINUX) 69 #if defined(OS_LINUX)
67 g_thread_init(NULL); 70 g_thread_init(NULL);
68 gtk_init_check(&argc, &argv); 71 gtk_init_check(&argc, &argv);
69 #endif 72 #endif
70 // Don't add additional code to this constructor. Instead add it to 73 // Don't add additional code to this constructor. Instead add it to
71 // Initialize(). See bug 6436. 74 // Initialize(). See bug 6436.
72 } 75 }
73 76
74 virtual ~TestSuite() { 77 virtual ~TestSuite() {
75 CommandLine::Terminate(); 78 CommandLine::Terminate();
76 } 79 }
77 80
81 // Returns true if a string starts with FLAKY_.
82 static bool IsFlaky(const char* name) {
83 return strncmp(name, "FLAKY_", 6) == 0;
84 }
85
86 // Returns true if the test is marked as flaky.
87 static bool FlakyTest(const testing::TestInfo& test) {
88 return IsFlaky(test.name()) || IsFlaky(test.test_case_name());
89 }
90
91 // Returns true if the test failed and is not marked as flaky.
92 static bool NonFlakyFailures(const testing::TestInfo& test) {
93 return test.should_run() && test.result()->Failed() && !FlakyTest(test);
94 }
95
96 // Returns the number of tests where the match function returns true.
97 int GetTestCount(TestMatch test_match) {
98 testing::UnitTest* instance = testing::UnitTest::GetInstance();
99 int count = 0;
100
101 for (int i = 0; i < instance->total_test_case_count(); ++i) {
102 const testing::TestCase& test_case = *instance->GetTestCase(i);
103 for (int j = 0; j < test_case.total_test_count(); ++j) {
104 if (test_match(*test_case.GetTestInfo(j))) {
105 count++;
106 }
107 }
108 }
109
110 return count;
111 }
112
78 // Don't add additional code to this method. Instead add it to 113 // Don't add additional code to this method. Instead add it to
79 // Initialize(). See bug 6436. 114 // Initialize(). See bug 6436.
80 int Run() { 115 int Run() {
81 base::ScopedNSAutoreleasePool scoped_pool; 116 base::ScopedNSAutoreleasePool scoped_pool;
82 117
83 Initialize(); 118 Initialize();
84 std::wstring client_func = 119 std::wstring client_func =
85 CommandLine::ForCurrentProcess()->GetSwitchValue(kRunClientProcess); 120 CommandLine::ForCurrentProcess()->GetSwitchValue(kRunClientProcess);
86 // Check to see if we are being run as a client process. 121 // Check to see if we are being run as a client process.
87 if (!client_func.empty()) { 122 if (!client_func.empty()) {
88 // Convert our function name to a usable string for GetProcAddress. 123 // Convert our function name to a usable string for GetProcAddress.
89 std::string func_name(client_func.begin(), client_func.end()); 124 std::string func_name(client_func.begin(), client_func.end());
90 125
91 return multi_process_function_list::InvokeChildProcessTest(func_name); 126 return multi_process_function_list::InvokeChildProcessTest(func_name);
92 } 127 }
93 int result = RUN_ALL_TESTS(); 128 int result = RUN_ALL_TESTS();
94 129
130 // Reset the result code if only flaky test failed.
131 if (result != 0 && GetTestCount(&TestSuite::NonFlakyFailures) == 0) {
132 result = 0;
133 }
134
135 // Display the number of flaky tests.
136 int flaky_count = GetTestCount(&TestSuite::FlakyTest);
137 printf(" YOU HAVE %d FLAKY %s\n\n", flaky_count,
138 flaky_count == 1 ? "TEST" : "TESTS");
139
95 // This MUST happen before Shutdown() since Shutdown() tears down 140 // This MUST happen before Shutdown() since Shutdown() tears down
96 // objects (such as NotificationService::current()) that Cocoa 141 // objects (such as NotificationService::current()) that Cocoa
97 // objects use to remove themselves as observers. 142 // objects use to remove themselves as observers.
98 scoped_pool.Recycle(); 143 scoped_pool.Recycle();
99 144
100 Shutdown(); 145 Shutdown();
101 146
102 return result; 147 return result;
103 } 148 }
104 149
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 226
182 virtual void Shutdown() { 227 virtual void Shutdown() {
183 } 228 }
184 229
185 // Make sure that we setup an AtExitManager so Singleton objects will be 230 // Make sure that we setup an AtExitManager so Singleton objects will be
186 // destroyed. 231 // destroyed.
187 base::AtExitManager at_exit_manager_; 232 base::AtExitManager at_exit_manager_;
188 }; 233 };
189 234
190 #endif // BASE_TEST_SUITE_H_ 235 #endif // BASE_TEST_SUITE_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698