OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/test/test_launcher.h" | 5 #include "base/test/test_launcher.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/bind.h" |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/environment.h" | 10 #include "base/environment.h" |
10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/format_macros.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
15 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
16 #include "base/time.h" | 18 #include "base/time.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
19 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
20 #include "base/mac/scoped_nsautorelease_pool.h" | 22 #include "base/mac/scoped_nsautorelease_pool.h" |
21 #endif | 23 #endif |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // unconditionally. | 101 // unconditionally. |
100 // Note: we don't output per-test-case or total summary info like | 102 // Note: we don't output per-test-case or total summary info like |
101 // total failed_test_count, disabled_test_count, elapsed_time and so on. | 103 // total failed_test_count, disabled_test_count, elapsed_time and so on. |
102 // Only each test (testcase element in the XML) will have the correct | 104 // Only each test (testcase element in the XML) will have the correct |
103 // failed/disabled/elapsed_time information. Each test won't include | 105 // failed/disabled/elapsed_time information. Each test won't include |
104 // detailed failure messages either. | 106 // detailed failure messages either. |
105 class ResultsPrinter { | 107 class ResultsPrinter { |
106 public: | 108 public: |
107 explicit ResultsPrinter(const CommandLine& command_line); | 109 explicit ResultsPrinter(const CommandLine& command_line); |
108 ~ResultsPrinter(); | 110 ~ResultsPrinter(); |
109 void OnTestCaseStart(const char* name, int test_count) const; | |
110 void OnTestCaseEnd() const; | |
111 | 111 |
112 void OnTestEnd(const char* name, const char* case_name, | 112 // Adds |result| to the stored test results. |
113 bool success, double elapsed_time) const; | 113 void AddTestResult(const TestResult& result); |
| 114 |
| 115 // Returns list of full names of failed tests. |
| 116 const std::vector<std::string>& failed_tests() const { return failed_tests_; } |
| 117 |
| 118 // Returns total number of tests run. |
| 119 size_t test_run_count() const { return test_run_count_; } |
| 120 |
114 private: | 121 private: |
| 122 // Test results grouped by test case name. |
| 123 typedef std::map<std::string, std::vector<TestResult> > ResultsMap; |
| 124 ResultsMap results_; |
| 125 |
| 126 // List of full names of failed tests. |
| 127 std::vector<std::string> failed_tests_; |
| 128 |
| 129 // Total number of tests run. |
| 130 size_t test_run_count_; |
| 131 |
| 132 // File handle of output file (can be NULL if no file). |
115 FILE* out_; | 133 FILE* out_; |
116 | 134 |
117 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); | 135 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); |
118 }; | 136 }; |
119 | 137 |
120 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) : out_(NULL) { | 138 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) |
| 139 : test_run_count_(0), |
| 140 out_(NULL) { |
121 if (!command_line.HasSwitch(kGTestOutputFlag)) | 141 if (!command_line.HasSwitch(kGTestOutputFlag)) |
122 return; | 142 return; |
123 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); | 143 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); |
124 size_t colon_pos = flag.find(':'); | 144 size_t colon_pos = flag.find(':'); |
125 FilePath path; | 145 FilePath path; |
126 if (colon_pos != std::string::npos) { | 146 if (colon_pos != std::string::npos) { |
127 FilePath flag_path = | 147 FilePath flag_path = |
128 command_line.GetSwitchValuePath(kGTestOutputFlag); | 148 command_line.GetSwitchValuePath(kGTestOutputFlag); |
129 FilePath::StringType path_string = flag_path.value(); | 149 FilePath::StringType path_string = flag_path.value(); |
130 path = FilePath(path_string.substr(colon_pos + 1)); | 150 path = FilePath(path_string.substr(colon_pos + 1)); |
(...skipping 14 matching lines...) Expand all Loading... |
145 << "Creating the directory: " << dir_name.value(); | 165 << "Creating the directory: " << dir_name.value(); |
146 // Create the directory if necessary (because the gtest does the same). | 166 // Create the directory if necessary (because the gtest does the same). |
147 file_util::CreateDirectory(dir_name); | 167 file_util::CreateDirectory(dir_name); |
148 } | 168 } |
149 out_ = file_util::OpenFile(path, "w"); | 169 out_ = file_util::OpenFile(path, "w"); |
150 if (!out_) { | 170 if (!out_) { |
151 LOG(ERROR) << "Cannot open output file: " | 171 LOG(ERROR) << "Cannot open output file: " |
152 << path.value() << "."; | 172 << path.value() << "."; |
153 return; | 173 return; |
154 } | 174 } |
155 fprintf(out_, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | |
156 fprintf(out_, "<testsuites name=\"AllTests\" tests=\"\" failures=\"\"" | |
157 " disabled=\"\" errors=\"\" time=\"\">\n"); | |
158 } | 175 } |
159 | 176 |
160 ResultsPrinter::~ResultsPrinter() { | 177 ResultsPrinter::~ResultsPrinter() { |
161 if (!out_) | 178 if (!out_) |
162 return; | 179 return; |
| 180 fprintf(out_, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
| 181 fprintf(out_, "<testsuites name=\"AllTests\" tests=\"\" failures=\"\"" |
| 182 " disabled=\"\" errors=\"\" time=\"\">\n"); |
| 183 for (ResultsMap::iterator i = results_.begin(); i != results_.end(); ++i) { |
| 184 fprintf(out_, " <testsuite name=\"%s\" tests=\"%" PRIuS "\" failures=\"\"" |
| 185 " disabled=\"\" errors=\"\" time=\"\">\n", |
| 186 i->first.c_str(), i->second.size()); |
| 187 for (size_t j = 0; j < i->second.size(); ++j) { |
| 188 const TestResult& result = i->second[j]; |
| 189 fprintf(out_, " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" |
| 190 " classname=\"%s\">\n", |
| 191 result.test_name.c_str(), |
| 192 result.elapsed_time.InSecondsF(), |
| 193 result.test_case_name.c_str()); |
| 194 if (!result.success) |
| 195 fprintf(out_, " <failure message=\"\" type=\"\"></failure>\n"); |
| 196 fprintf(out_, " </testcase>\n"); |
| 197 } |
| 198 fprintf(out_, " </testsuite>\n"); |
| 199 } |
163 fprintf(out_, "</testsuites>\n"); | 200 fprintf(out_, "</testsuites>\n"); |
164 fclose(out_); | 201 fclose(out_); |
165 } | 202 } |
166 | 203 |
167 void ResultsPrinter::OnTestCaseStart(const char* name, int test_count) const { | 204 void ResultsPrinter::AddTestResult(const TestResult& result) { |
168 if (!out_) | 205 ++test_run_count_; |
169 return; | 206 results_[result.test_case_name].push_back(result); |
170 fprintf(out_, " <testsuite name=\"%s\" tests=\"%d\" failures=\"\"" | 207 |
171 " disabled=\"\" errors=\"\" time=\"\">\n", name, test_count); | 208 if (!result.success) { |
| 209 failed_tests_.push_back( |
| 210 std::string(result.test_case_name) + "." + result.test_name); |
| 211 } |
172 } | 212 } |
173 | 213 |
174 void ResultsPrinter::OnTestCaseEnd() const { | |
175 if (!out_) | |
176 return; | |
177 fprintf(out_, " </testsuite>\n"); | |
178 } | |
179 | |
180 void ResultsPrinter::OnTestEnd(const char* name, | |
181 const char* case_name, | |
182 bool success, | |
183 double elapsed_time) const { | |
184 if (!out_) | |
185 return; | |
186 fprintf(out_, " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" | |
187 " classname=\"%s\">\n", | |
188 name, elapsed_time / 1000.0, case_name); | |
189 if (!success) | |
190 fprintf(out_, " <failure message=\"\" type=\"\"></failure>\n"); | |
191 fprintf(out_, "</testcase>\n"); | |
192 } | |
193 | |
194 class TestCasePrinterHelper { | |
195 public: | |
196 TestCasePrinterHelper(const ResultsPrinter& printer, | |
197 const char* name, | |
198 int total_test_count) | |
199 : printer_(printer) { | |
200 printer_.OnTestCaseStart(name, total_test_count); | |
201 } | |
202 ~TestCasePrinterHelper() { | |
203 printer_.OnTestCaseEnd(); | |
204 } | |
205 private: | |
206 const ResultsPrinter& printer_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(TestCasePrinterHelper); | |
209 }; | |
210 | |
211 // For a basic pattern matching for gtest_filter options. (Copied from | 214 // For a basic pattern matching for gtest_filter options. (Copied from |
212 // gtest.cc, see the comment below and http://crbug.com/44497) | 215 // gtest.cc, see the comment below and http://crbug.com/44497) |
213 bool PatternMatchesString(const char* pattern, const char* str) { | 216 bool PatternMatchesString(const char* pattern, const char* str) { |
214 switch (*pattern) { | 217 switch (*pattern) { |
215 case '\0': | 218 case '\0': |
216 case ':': // Either ':' or '\0' marks the end of the pattern. | 219 case ':': // Either ':' or '\0' marks the end of the pattern. |
217 return *str == '\0'; | 220 return *str == '\0'; |
218 case '?': // Matches any single character. | 221 case '?': // Matches any single character. |
219 return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); | 222 return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); |
220 case '*': // Matches any string (possibly empty) of characters. | 223 case '*': // Matches any string (possibly empty) of characters. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 // positive filter and negative filter portions. | 267 // positive filter and negative filter portions. |
265 std::string positive_filter = filter; | 268 std::string positive_filter = filter; |
266 std::string negative_filter; | 269 std::string negative_filter; |
267 size_t dash_pos = filter.find('-'); | 270 size_t dash_pos = filter.find('-'); |
268 if (dash_pos != std::string::npos) { | 271 if (dash_pos != std::string::npos) { |
269 positive_filter = filter.substr(0, dash_pos); // Everything up to the dash. | 272 positive_filter = filter.substr(0, dash_pos); // Everything up to the dash. |
270 negative_filter = filter.substr(dash_pos + 1); // Everything after the dash. | 273 negative_filter = filter.substr(dash_pos + 1); // Everything after the dash. |
271 } | 274 } |
272 | 275 |
273 int num_runnable_tests = 0; | 276 int num_runnable_tests = 0; |
274 int test_run_count = 0; | |
275 std::vector<std::string> failed_tests; | |
276 | 277 |
277 ResultsPrinter printer(*command_line); | 278 ResultsPrinter printer(*command_line); |
278 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { | 279 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { |
279 const testing::TestCase* test_case = unit_test->GetTestCase(i); | 280 const testing::TestCase* test_case = unit_test->GetTestCase(i); |
280 TestCasePrinterHelper helper(printer, test_case->name(), | |
281 test_case->total_test_count()); | |
282 for (int j = 0; j < test_case->total_test_count(); ++j) { | 281 for (int j = 0; j < test_case->total_test_count(); ++j) { |
283 const testing::TestInfo* test_info = test_case->GetTestInfo(j); | 282 const testing::TestInfo* test_info = test_case->GetTestInfo(j); |
284 std::string test_name = test_info->test_case_name(); | 283 std::string test_name = test_info->test_case_name(); |
285 test_name.append("."); | 284 test_name.append("."); |
286 test_name.append(test_info->name()); | 285 test_name.append(test_info->name()); |
287 | 286 |
288 // Skip disabled tests. | 287 // Skip disabled tests. |
289 if (test_name.find("DISABLED") != std::string::npos && | 288 if (test_name.find("DISABLED") != std::string::npos && |
290 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | 289 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { |
291 continue; | 290 continue; |
292 } | 291 } |
293 | 292 |
294 // Skip the test that doesn't match the filter string (if given). | 293 // Skip the test that doesn't match the filter string (if given). |
295 if ((!positive_filter.empty() && | 294 if ((!positive_filter.empty() && |
296 !MatchesFilter(test_name, positive_filter)) || | 295 !MatchesFilter(test_name, positive_filter)) || |
297 MatchesFilter(test_name, negative_filter)) { | 296 MatchesFilter(test_name, negative_filter)) { |
298 continue; | 297 continue; |
299 } | 298 } |
300 | 299 |
301 if (!launcher_delegate->ShouldRunTest(test_case, test_info)) | 300 if (!launcher_delegate->ShouldRunTest(test_case, test_info)) |
302 continue; | 301 continue; |
303 | 302 |
304 bool should_run = ShouldRunTestOnShard(total_shards, shard_index, | 303 bool should_run = ShouldRunTestOnShard(total_shards, shard_index, |
305 num_runnable_tests); | 304 num_runnable_tests); |
306 num_runnable_tests += 1; | 305 num_runnable_tests += 1; |
307 if (!should_run) | 306 if (!should_run) |
308 continue; | 307 continue; |
309 | 308 |
310 TimeTicks start_time = TimeTicks::Now(); | 309 launcher_delegate->RunTest(test_case, |
311 ++test_run_count; | 310 test_info, |
312 bool success = launcher_delegate->RunTest(test_case, test_info); | 311 base::Bind( |
313 if (!success) | 312 &ResultsPrinter::AddTestResult, |
314 failed_tests.push_back(test_name); | 313 base::Unretained(&printer))); |
315 printer.OnTestEnd( | |
316 test_info->name(), test_case->name(), success, | |
317 (TimeTicks::Now() - start_time).InMillisecondsF()); | |
318 } | 314 } |
319 } | 315 } |
320 | 316 |
321 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); | 317 launcher_delegate->RunRemainingTests(); |
322 printf("%d test%s failed\n", | 318 |
323 static_cast<int>(failed_tests.size()), | 319 printf("%" PRIuS " test%s run\n", |
324 failed_tests.size() != 1 ? "s" : ""); | 320 printer.test_run_count(), |
325 if (failed_tests.empty()) | 321 printer.test_run_count() > 1 ? "s" : ""); |
| 322 printf("%" PRIuS " test%s failed\n", |
| 323 printer.failed_tests().size(), |
| 324 printer.failed_tests().size() != 1 ? "s" : ""); |
| 325 if (printer.failed_tests().empty()) |
326 return true; | 326 return true; |
327 | 327 |
328 printf("Failing tests:\n"); | 328 printf("Failing tests:\n"); |
329 for (std::vector<std::string>::const_iterator iter = failed_tests.begin(); | 329 for (size_t i = 0; i < printer.failed_tests().size(); ++i) |
330 iter != failed_tests.end(); ++iter) { | 330 printf("%s\n", printer.failed_tests()[i].c_str()); |
331 printf("%s\n", iter->c_str()); | |
332 } | |
333 | 331 |
334 return false; | 332 return false; |
335 } | 333 } |
336 | 334 |
337 } // namespace | 335 } // namespace |
338 | 336 |
339 const char kGTestFilterFlag[] = "gtest_filter"; | 337 const char kGTestFilterFlag[] = "gtest_filter"; |
340 const char kGTestListTestsFlag[] = "gtest_list_tests"; | 338 const char kGTestListTestsFlag[] = "gtest_list_tests"; |
341 const char kGTestRepeatFlag[] = "gtest_repeat"; | 339 const char kGTestRepeatFlag[] = "gtest_repeat"; |
342 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests"; | 340 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests"; |
343 const char kGTestOutputFlag[] = "gtest_output"; | 341 const char kGTestOutputFlag[] = "gtest_output"; |
344 | 342 |
345 const char kHelpFlag[] = "help"; | 343 const char kHelpFlag[] = "help"; |
346 | 344 |
| 345 TestResult::TestResult() { |
| 346 } |
| 347 |
347 TestLauncherDelegate::~TestLauncherDelegate() { | 348 TestLauncherDelegate::~TestLauncherDelegate() { |
348 } | 349 } |
349 | 350 |
350 int LaunchTests(TestLauncherDelegate* launcher_delegate, | 351 int LaunchTests(TestLauncherDelegate* launcher_delegate, |
351 int argc, | 352 int argc, |
352 char** argv) { | 353 char** argv) { |
353 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 354 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
354 | 355 |
355 int32 total_shards; | 356 int32 total_shards; |
356 int32 shard_index; | 357 int32 shard_index; |
(...skipping 12 matching lines...) Expand all Loading... |
369 | 370 |
370 // Special value "-1" means "repeat indefinitely". | 371 // Special value "-1" means "repeat indefinitely". |
371 if (cycles != -1) | 372 if (cycles != -1) |
372 cycles--; | 373 cycles--; |
373 } | 374 } |
374 | 375 |
375 return exit_code; | 376 return exit_code; |
376 } | 377 } |
377 | 378 |
378 } // namespace base | 379 } // namespace base |
OLD | NEW |