OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
11 #include "base/linked_ptr.h" | 11 #include "base/linked_ptr.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 // Note: as currently XML is the only supported format by gtest, we don't | 78 // Note: as currently XML is the only supported format by gtest, we don't |
79 // check output format (e.g. "xml:" prefix) here and output an XML file | 79 // check output format (e.g. "xml:" prefix) here and output an XML file |
80 // unconditionally. | 80 // unconditionally. |
81 // Note: we don't output per-test-case or total summary info like | 81 // Note: we don't output per-test-case or total summary info like |
82 // total failed_test_count, disabled_test_count, elapsed_time and so on. | 82 // total failed_test_count, disabled_test_count, elapsed_time and so on. |
83 // Only each test (testcase element in the XML) will have the correct | 83 // Only each test (testcase element in the XML) will have the correct |
84 // failed/disabled/elapsed_time information. Each test won't include | 84 // failed/disabled/elapsed_time information. Each test won't include |
85 // detailed failure messages either. | 85 // detailed failure messages either. |
86 class ResultsPrinter { | 86 class ResultsPrinter { |
87 public: | 87 public: |
88 enum ResultMask { | |
Paweł Hajdan Jr.
2011/01/11 16:33:11
Why do we need a mask? It should be possible to ju
tfarina
2011/01/11 16:36:49
See line 419.
| |
89 RESULT_NONE = 0, | |
90 RESULT_FAILED = 1 << 0, | |
91 RESULT_FAILURE_IGNORED = 1 << 1, | |
92 }; | |
93 | |
88 explicit ResultsPrinter(const CommandLine& command_line); | 94 explicit ResultsPrinter(const CommandLine& command_line); |
89 ~ResultsPrinter(); | 95 ~ResultsPrinter(); |
90 void OnTestCaseStart(const char* name, int test_count) const; | 96 void OnTestCaseStart(const char* name, int test_count) const; |
91 void OnTestCaseEnd() const; | 97 void OnTestCaseEnd() const; |
92 | 98 |
93 // TODO(phajdan.jr): Convert bool failed, bool failure_ignored to an enum. | |
94 void OnTestEnd(const char* name, const char* case_name, bool run, | 99 void OnTestEnd(const char* name, const char* case_name, bool run, |
95 bool failed, bool failure_ignored, double elapsed_time) const; | 100 int result_mask, double elapsed_time) const; |
96 private: | 101 private: |
97 FILE* out_; | 102 FILE* out_; |
98 | 103 |
99 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); | 104 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); |
100 }; | 105 }; |
101 | 106 |
102 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) : out_(NULL) { | 107 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) : out_(NULL) { |
103 if (!command_line.HasSwitch(kGTestOutputFlag)) | 108 if (!command_line.HasSwitch(kGTestOutputFlag)) |
104 return; | 109 return; |
105 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); | 110 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 | 159 |
155 void ResultsPrinter::OnTestCaseEnd() const { | 160 void ResultsPrinter::OnTestCaseEnd() const { |
156 if (!out_) | 161 if (!out_) |
157 return; | 162 return; |
158 fprintf(out_, " </testsuite>\n"); | 163 fprintf(out_, " </testsuite>\n"); |
159 } | 164 } |
160 | 165 |
161 void ResultsPrinter::OnTestEnd(const char* name, | 166 void ResultsPrinter::OnTestEnd(const char* name, |
162 const char* case_name, | 167 const char* case_name, |
163 bool run, | 168 bool run, |
164 bool failed, | 169 int result_mask, |
165 bool failure_ignored, | |
166 double elapsed_time) const { | 170 double elapsed_time) const { |
167 if (!out_) | 171 if (!out_) |
168 return; | 172 return; |
169 fprintf(out_, " <testcase name=\"%s\" status=\"%s\" time=\"%.3f\"" | 173 fprintf(out_, " <testcase name=\"%s\" status=\"%s\" time=\"%.3f\"" |
170 " classname=\"%s\"", | 174 " classname=\"%s\"", |
171 name, run ? "run" : "notrun", elapsed_time / 1000.0, case_name); | 175 name, run ? "run" : "notrun", elapsed_time / 1000.0, case_name); |
172 if (!failed) { | 176 if (!(result_mask & FAILED)) { |
173 fprintf(out_, " />\n"); | 177 fprintf(out_, " />\n"); |
174 return; | 178 return; |
175 } | 179 } |
176 fprintf(out_, ">\n"); | 180 fprintf(out_, ">\n"); |
177 fprintf(out_, " <failure message=\"\" type=\"\"%s></failure>\n", | 181 fprintf(out_, " <failure message=\"\" type=\"\"%s></failure>\n", |
178 failure_ignored ? " ignored=\"true\"" : ""); | 182 (result_mask & FAILURE_IGNORED) ? " ignored=\"true\"" : ""); |
179 fprintf(out_, " </testcase>\n"); | 183 fprintf(out_, " </testcase>\n"); |
180 } | 184 } |
181 | 185 |
182 class TestCasePrinterHelper { | 186 class TestCasePrinterHelper { |
183 public: | 187 public: |
184 TestCasePrinterHelper(const ResultsPrinter& printer, | 188 TestCasePrinterHelper(const ResultsPrinter& printer, |
185 const char* name, | 189 const char* name, |
186 int total_test_count) | 190 int total_test_count) |
187 : printer_(printer) { | 191 : printer_(printer) { |
188 printer_.OnTestCaseStart(name, total_test_count); | 192 printer_.OnTestCaseStart(name, total_test_count); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 ResultsPrinter printer(*command_line); | 376 ResultsPrinter printer(*command_line); |
373 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { | 377 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { |
374 const testing::TestCase* test_case = unit_test->GetTestCase(i); | 378 const testing::TestCase* test_case = unit_test->GetTestCase(i); |
375 TestCasePrinterHelper helper(printer, test_case->name(), | 379 TestCasePrinterHelper helper(printer, test_case->name(), |
376 test_case->total_test_count()); | 380 test_case->total_test_count()); |
377 for (int j = 0; j < test_case->total_test_count(); ++j) { | 381 for (int j = 0; j < test_case->total_test_count(); ++j) { |
378 const testing::TestInfo* test_info = test_case->GetTestInfo(j); | 382 const testing::TestInfo* test_info = test_case->GetTestInfo(j); |
379 // Skip disabled tests. | 383 // Skip disabled tests. |
380 if (std::string(test_info->name()).find("DISABLED") == 0 && | 384 if (std::string(test_info->name()).find("DISABLED") == 0 && |
381 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | 385 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { |
382 printer.OnTestEnd(test_info->name(), test_case->name(), | 386 printer.OnTestEnd(test_info->name(), test_case->name(), false, |
383 false, false, false, 0); | 387 ResultsPrinter::RESULT_NONE, 0); |
384 continue; | 388 continue; |
385 } | 389 } |
386 std::string test_name = test_info->test_case_name(); | 390 std::string test_name = test_info->test_case_name(); |
387 test_name.append("."); | 391 test_name.append("."); |
388 test_name.append(test_info->name()); | 392 test_name.append(test_info->name()); |
389 // Skip the test that doesn't match the filter string (if given). | 393 // Skip the test that doesn't match the filter string (if given). |
390 if ((!positive_filter.empty() && | 394 if ((!positive_filter.empty() && |
391 !MatchesFilter(test_name, positive_filter)) || | 395 !MatchesFilter(test_name, positive_filter)) || |
392 MatchesFilter(test_name, negative_filter)) { | 396 MatchesFilter(test_name, negative_filter)) { |
393 printer.OnTestEnd(test_info->name(), test_case->name(), | 397 printer.OnTestEnd(test_info->name(), test_case->name(), false, |
394 false, false, false, 0); | 398 ResultsPrinter::RESULT_NONE, 0); |
395 continue; | 399 continue; |
396 } | 400 } |
397 base::Time start_time = base::Time::Now(); | 401 base::Time start_time = base::Time::Now(); |
398 ++test_run_count; | 402 ++test_run_count; |
399 int exit_code = RunTest(test_name); | 403 int exit_code = RunTest(test_name); |
400 if (exit_code == 0) { | 404 if (exit_code == 0) { |
401 // Test passed. | 405 // Test passed. |
402 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, | 406 printer.OnTestEnd(test_info->name(), test_case->name(), true, |
403 false, | 407 ResultsPrinter::RESULT_NONE, |
404 (base::Time::Now() - start_time).InMillisecondsF()); | 408 (base::Time::Now() - start_time).InMillisecondsF()); |
405 } else { | 409 } else { |
406 failed_tests.push_back(test_name); | 410 failed_tests.push_back(test_name); |
407 | 411 |
408 bool ignore_failure = false; | 412 bool ignore_failure = false; |
409 | 413 |
410 // -1 exit code means a crash or hang. Never ignore those failures, | 414 // -1 exit code means a crash or hang. Never ignore those failures, |
411 // they are serious and should always be visible. | 415 // they are serious and should always be visible. |
412 if (exit_code != -1) | 416 if (exit_code != -1) |
413 ignore_failure = base::TestSuite::ShouldIgnoreFailure(*test_info); | 417 ignore_failure = base::TestSuite::ShouldIgnoreFailure(*test_info); |
414 | 418 |
415 printer.OnTestEnd(test_info->name(), test_case->name(), true, true, | 419 int results = ResultsPrinter::RESULT_FAILED; |
416 ignore_failure, | 420 if (ignore_failure) |
421 results |= ResultsPrinter::RESULT_FAILURE_IGNORED; | |
422 | |
423 printer.OnTestEnd(test_info->name(), test_case->name(), true, results, | |
tfarina
2011/01/11 16:54:04
If it was an enum, how you would write this line?
Paweł Hajdan Jr.
2011/01/11 16:55:18
Exactly the same.
| |
417 (base::Time::Now() - start_time).InMillisecondsF()); | 424 (base::Time::Now() - start_time).InMillisecondsF()); |
418 if (ignore_failure) | 425 if (ignore_failure) |
419 ++ignored_failure_count; | 426 ++ignored_failure_count; |
420 } | 427 } |
421 } | 428 } |
422 } | 429 } |
423 | 430 |
424 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); | 431 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); |
425 printf("%d test%s failed (%d ignored)\n", | 432 printf("%d test%s failed (%d ignored)\n", |
426 static_cast<int>(failed_tests.size()), | 433 static_cast<int>(failed_tests.size()), |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 exit_code = 1; | 544 exit_code = 1; |
538 break; | 545 break; |
539 } | 546 } |
540 | 547 |
541 // Special value "-1" means "repeat indefinitely". | 548 // Special value "-1" means "repeat indefinitely". |
542 if (cycles != -1) | 549 if (cycles != -1) |
543 cycles--; | 550 cycles--; |
544 } | 551 } |
545 return exit_code; | 552 return exit_code; |
546 } | 553 } |
OLD | NEW |