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 void AddTestResult(const TestResult& result); |
sky
2013/06/21 19:46:08
How about descriptions for some of these functions
Paweł Hajdan Jr.
2013/06/21 20:56:57
Done.
| |
113 bool success, double elapsed_time) const; | 113 |
114 const std::vector<std::string>& failed_tests() const { return failed_tests_; } | |
115 | |
116 size_t test_run_count() const { return test_run_count_; } | |
117 | |
114 private: | 118 private: |
119 typedef std::map<std::string, std::vector<TestResult> > ResultsMap; | |
120 ResultsMap results_; | |
121 | |
122 std::vector<std::string> failed_tests_; | |
123 | |
124 size_t test_run_count_; | |
125 | |
115 FILE* out_; | 126 FILE* out_; |
116 | 127 |
117 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); | 128 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); |
118 }; | 129 }; |
119 | 130 |
120 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) : out_(NULL) { | 131 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) |
132 : test_run_count_(0), | |
133 out_(NULL) { | |
121 if (!command_line.HasSwitch(kGTestOutputFlag)) | 134 if (!command_line.HasSwitch(kGTestOutputFlag)) |
122 return; | 135 return; |
123 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); | 136 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); |
124 size_t colon_pos = flag.find(':'); | 137 size_t colon_pos = flag.find(':'); |
125 FilePath path; | 138 FilePath path; |
126 if (colon_pos != std::string::npos) { | 139 if (colon_pos != std::string::npos) { |
127 FilePath flag_path = | 140 FilePath flag_path = |
128 command_line.GetSwitchValuePath(kGTestOutputFlag); | 141 command_line.GetSwitchValuePath(kGTestOutputFlag); |
129 FilePath::StringType path_string = flag_path.value(); | 142 FilePath::StringType path_string = flag_path.value(); |
130 path = FilePath(path_string.substr(colon_pos + 1)); | 143 path = FilePath(path_string.substr(colon_pos + 1)); |
(...skipping 14 matching lines...) Expand all Loading... | |
145 << "Creating the directory: " << dir_name.value(); | 158 << "Creating the directory: " << dir_name.value(); |
146 // Create the directory if necessary (because the gtest does the same). | 159 // Create the directory if necessary (because the gtest does the same). |
147 file_util::CreateDirectory(dir_name); | 160 file_util::CreateDirectory(dir_name); |
148 } | 161 } |
149 out_ = file_util::OpenFile(path, "w"); | 162 out_ = file_util::OpenFile(path, "w"); |
150 if (!out_) { | 163 if (!out_) { |
151 LOG(ERROR) << "Cannot open output file: " | 164 LOG(ERROR) << "Cannot open output file: " |
152 << path.value() << "."; | 165 << path.value() << "."; |
153 return; | 166 return; |
154 } | 167 } |
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 } | 168 } |
159 | 169 |
160 ResultsPrinter::~ResultsPrinter() { | 170 ResultsPrinter::~ResultsPrinter() { |
161 if (!out_) | 171 if (!out_) |
162 return; | 172 return; |
173 fprintf(out_, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | |
174 fprintf(out_, "<testsuites name=\"AllTests\" tests=\"\" failures=\"\"" | |
175 " disabled=\"\" errors=\"\" time=\"\">\n"); | |
176 for (ResultsMap::iterator i = results_.begin(); i != results_.end(); ++i) { | |
177 fprintf(out_, " <testsuite name=\"%s\" tests=\"%" PRIuS "\" failures=\"\"" | |
178 " disabled=\"\" errors=\"\" time=\"\">\n", | |
179 i->first.c_str(), i->second.size()); | |
180 for (size_t j = 0; j < i->second.size(); ++j) { | |
181 const TestResult& result = i->second[j]; | |
182 fprintf(out_, " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" | |
183 " classname=\"%s\">\n", | |
184 result.test_name.c_str(), | |
185 result.elapsed_time.InMillisecondsF() / 1000.0, | |
186 result.test_case_name.c_str()); | |
187 if (!result.success) | |
188 fprintf(out_, " <failure message=\"\" type=\"\"></failure>\n"); | |
189 fprintf(out_, " </testcase>\n"); | |
190 } | |
191 fprintf(out_, " </testsuite>\n"); | |
192 } | |
163 fprintf(out_, "</testsuites>\n"); | 193 fprintf(out_, "</testsuites>\n"); |
164 fclose(out_); | 194 fclose(out_); |
165 } | 195 } |
166 | 196 |
167 void ResultsPrinter::OnTestCaseStart(const char* name, int test_count) const { | 197 void ResultsPrinter::AddTestResult(const TestResult& result) { |
168 if (!out_) | 198 ++test_run_count_; |
169 return; | 199 results_[result.test_case_name].push_back(result); |
170 fprintf(out_, " <testsuite name=\"%s\" tests=\"%d\" failures=\"\"" | 200 |
171 " disabled=\"\" errors=\"\" time=\"\">\n", name, test_count); | 201 if (!result.success) { |
202 failed_tests_.push_back( | |
203 std::string(result.test_case_name) + "." + result.test_name); | |
204 } | |
172 } | 205 } |
173 | 206 |
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 | 207 // For a basic pattern matching for gtest_filter options. (Copied from |
212 // gtest.cc, see the comment below and http://crbug.com/44497) | 208 // gtest.cc, see the comment below and http://crbug.com/44497) |
213 bool PatternMatchesString(const char* pattern, const char* str) { | 209 bool PatternMatchesString(const char* pattern, const char* str) { |
214 switch (*pattern) { | 210 switch (*pattern) { |
215 case '\0': | 211 case '\0': |
216 case ':': // Either ':' or '\0' marks the end of the pattern. | 212 case ':': // Either ':' or '\0' marks the end of the pattern. |
217 return *str == '\0'; | 213 return *str == '\0'; |
218 case '?': // Matches any single character. | 214 case '?': // Matches any single character. |
219 return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); | 215 return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); |
220 case '*': // Matches any string (possibly empty) of characters. | 216 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. | 260 // positive filter and negative filter portions. |
265 std::string positive_filter = filter; | 261 std::string positive_filter = filter; |
266 std::string negative_filter; | 262 std::string negative_filter; |
267 size_t dash_pos = filter.find('-'); | 263 size_t dash_pos = filter.find('-'); |
268 if (dash_pos != std::string::npos) { | 264 if (dash_pos != std::string::npos) { |
269 positive_filter = filter.substr(0, dash_pos); // Everything up to the dash. | 265 positive_filter = filter.substr(0, dash_pos); // Everything up to the dash. |
270 negative_filter = filter.substr(dash_pos + 1); // Everything after the dash. | 266 negative_filter = filter.substr(dash_pos + 1); // Everything after the dash. |
271 } | 267 } |
272 | 268 |
273 int num_runnable_tests = 0; | 269 int num_runnable_tests = 0; |
274 int test_run_count = 0; | |
275 std::vector<std::string> failed_tests; | |
276 | 270 |
277 ResultsPrinter printer(*command_line); | 271 ResultsPrinter printer(*command_line); |
278 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { | 272 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { |
279 const testing::TestCase* test_case = unit_test->GetTestCase(i); | 273 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) { | 274 for (int j = 0; j < test_case->total_test_count(); ++j) { |
283 const testing::TestInfo* test_info = test_case->GetTestInfo(j); | 275 const testing::TestInfo* test_info = test_case->GetTestInfo(j); |
284 std::string test_name = test_info->test_case_name(); | 276 std::string test_name = test_info->test_case_name(); |
285 test_name.append("."); | 277 test_name.append("."); |
286 test_name.append(test_info->name()); | 278 test_name.append(test_info->name()); |
287 | 279 |
288 // Skip disabled tests. | 280 // Skip disabled tests. |
289 if (test_name.find("DISABLED") != std::string::npos && | 281 if (test_name.find("DISABLED") != std::string::npos && |
290 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | 282 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { |
291 continue; | 283 continue; |
292 } | 284 } |
293 | 285 |
294 // Skip the test that doesn't match the filter string (if given). | 286 // Skip the test that doesn't match the filter string (if given). |
295 if ((!positive_filter.empty() && | 287 if ((!positive_filter.empty() && |
296 !MatchesFilter(test_name, positive_filter)) || | 288 !MatchesFilter(test_name, positive_filter)) || |
297 MatchesFilter(test_name, negative_filter)) { | 289 MatchesFilter(test_name, negative_filter)) { |
298 continue; | 290 continue; |
299 } | 291 } |
300 | 292 |
301 if (!launcher_delegate->ShouldRunTest(test_case, test_info)) | 293 if (!launcher_delegate->ShouldRunTest(test_case, test_info)) |
302 continue; | 294 continue; |
303 | 295 |
304 bool should_run = ShouldRunTestOnShard(total_shards, shard_index, | 296 bool should_run = ShouldRunTestOnShard(total_shards, shard_index, |
305 num_runnable_tests); | 297 num_runnable_tests); |
306 num_runnable_tests += 1; | 298 num_runnable_tests += 1; |
307 if (!should_run) | 299 if (!should_run) |
308 continue; | 300 continue; |
309 | 301 |
310 TimeTicks start_time = TimeTicks::Now(); | 302 launcher_delegate->RunTest(test_case, |
311 ++test_run_count; | 303 test_info, |
312 bool success = launcher_delegate->RunTest(test_case, test_info); | 304 base::Bind( |
313 if (!success) | 305 &ResultsPrinter::AddTestResult, |
314 failed_tests.push_back(test_name); | 306 base::Unretained(&printer))); |
315 printer.OnTestEnd( | |
316 test_info->name(), test_case->name(), success, | |
317 (TimeTicks::Now() - start_time).InMillisecondsF()); | |
318 } | 307 } |
319 } | 308 } |
320 | 309 |
321 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); | 310 launcher_delegate->RunRemainingTests(); |
322 printf("%d test%s failed\n", | 311 |
323 static_cast<int>(failed_tests.size()), | 312 printf("%" PRIuS " test%s run\n", |
324 failed_tests.size() != 1 ? "s" : ""); | 313 printer.test_run_count(), |
325 if (failed_tests.empty()) | 314 printer.test_run_count() > 1 ? "s" : ""); |
315 printf("%" PRIuS " test%s failed\n", | |
316 printer.failed_tests().size(), | |
317 printer.failed_tests().size() != 1 ? "s" : ""); | |
318 if (printer.failed_tests().empty()) | |
326 return true; | 319 return true; |
327 | 320 |
328 printf("Failing tests:\n"); | 321 printf("Failing tests:\n"); |
329 for (std::vector<std::string>::const_iterator iter = failed_tests.begin(); | 322 for (size_t i = 0; i < printer.failed_tests().size(); ++i) |
330 iter != failed_tests.end(); ++iter) { | 323 printf("%s\n", printer.failed_tests()[i].c_str()); |
331 printf("%s\n", iter->c_str()); | |
332 } | |
333 | 324 |
334 return false; | 325 return false; |
335 } | 326 } |
336 | 327 |
337 } // namespace | 328 } // namespace |
338 | 329 |
339 const char kGTestFilterFlag[] = "gtest_filter"; | 330 const char kGTestFilterFlag[] = "gtest_filter"; |
340 const char kGTestListTestsFlag[] = "gtest_list_tests"; | 331 const char kGTestListTestsFlag[] = "gtest_list_tests"; |
341 const char kGTestRepeatFlag[] = "gtest_repeat"; | 332 const char kGTestRepeatFlag[] = "gtest_repeat"; |
342 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests"; | 333 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests"; |
(...skipping 26 matching lines...) Expand all Loading... | |
369 | 360 |
370 // Special value "-1" means "repeat indefinitely". | 361 // Special value "-1" means "repeat indefinitely". |
371 if (cycles != -1) | 362 if (cycles != -1) |
372 cycles--; | 363 cycles--; |
373 } | 364 } |
374 | 365 |
375 return exit_code; | 366 return exit_code; |
376 } | 367 } |
377 | 368 |
378 } // namespace base | 369 } // namespace base |
OLD | NEW |