OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 | |
5 #include "chrome/test/browser/browser_test_runner.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | |
11 #include "base/process_util.h" | |
12 #include "base/scoped_ptr.h" | |
13 #include "base/string_util.h" | |
14 | |
15 namespace { | |
16 | |
17 const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; | |
18 const wchar_t* const kGTestRunDisabledTestsFlag = | |
19 L"gtest_also_run_disabled_tests"; | |
20 | |
21 // Retrieves the list of tests to run by running gtest with the | |
22 // --gtest_list_tests flag in a forked process and parsing its output. | |
23 // |command_line| should contain the command line used to start the browser | |
24 // test launcher, it is expected that it does not contain the | |
25 // --gtest_list_tests flag already. | |
26 // Note: we cannot implement this in-process for InProcessBrowserTestRunner as | |
27 // GTest prints to the stdout and there are no good way of temporarily | |
28 // redirecting outputs. | |
29 bool GetTestList(const CommandLine& command_line, | |
30 std::vector<std::string>* test_list) { | |
31 DCHECK(!command_line.HasSwitch(kGTestListTestsFlag)); | |
32 | |
33 // Run ourselves with the --gtest_list_tests option and read the output. | |
34 CommandLine new_command_line(command_line); | |
35 new_command_line.AppendSwitch(kGTestListTestsFlag); | |
36 std::string output; | |
37 if (!base::GetAppOutput(new_command_line, &output)) | |
38 return false; | |
39 | |
40 // The output looks like: | |
41 // TestCase. | |
42 // Test1 | |
43 // Test2 | |
44 // OtherTestCase. | |
45 // FooTest | |
46 // ... | |
47 std::vector<std::string> lines; | |
48 SplitString(output, '\n', &lines); | |
49 | |
50 std::string test_case; | |
51 for (std::vector<std::string>::const_iterator iter = lines.begin(); | |
52 iter != lines.end(); ++iter) { | |
53 std::string line = *iter; | |
54 if (line.empty()) | |
55 continue; // Just ignore empty lines if any. | |
56 | |
57 if (line[line.size() - 1] == '.') { | |
58 // This is a new test case. | |
59 test_case = line; | |
60 continue; | |
61 } | |
62 | |
63 if (!command_line.HasSwitch(kGTestRunDisabledTestsFlag) && | |
64 line.find("DISABLED") != std::string::npos) | |
65 continue; // Skip disabled tests. | |
66 | |
67 // We are dealing with a test. | |
68 test_list->push_back(test_case + line); | |
69 } | |
70 return true; | |
71 } | |
72 | |
73 } // namespace | |
74 | |
75 namespace browser_tests { | |
76 | |
77 BrowserTestRunner::BrowserTestRunner() { | |
78 } | |
79 | |
80 BrowserTestRunner::~BrowserTestRunner() { | |
81 } | |
82 | |
83 bool RunTests(const BrowserTestRunnerFactory& browser_test_runner_factory) { | |
84 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
85 | |
86 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); | |
87 | |
88 // First let's get the list of tests we need to run. | |
89 std::vector<std::string> test_list; | |
90 if (!GetTestList(*command_line, &test_list)) { | |
91 printf("Failed to retrieve the tests to run.\n"); | |
92 return false; | |
93 } | |
94 | |
95 if (test_list.empty()) { | |
96 printf("No tests to run.\n"); | |
97 return false; | |
98 } | |
99 | |
100 int test_run_count = 0; | |
101 std::vector<std::string> failed_tests; | |
102 for (std::vector<std::string>::const_iterator iter = test_list.begin(); | |
103 iter != test_list.end(); ++iter) { | |
104 std::string test_name = *iter; | |
105 scoped_ptr<BrowserTestRunner> test_runner( | |
106 browser_test_runner_factory.CreateBrowserTestRunner()); | |
107 if (!test_runner.get() || !test_runner->Init()) | |
108 return false; | |
109 test_run_count++; | |
110 if (!test_runner->RunTest(test_name.c_str())) { | |
111 if (std::find(failed_tests.begin(), failed_tests.end(), test_name) == | |
112 failed_tests.end()) { | |
113 failed_tests.push_back(*iter); | |
114 } | |
115 } | |
116 } | |
117 | |
118 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); | |
119 printf("%d test%s failed\n", static_cast<int>(failed_tests.size()), | |
120 failed_tests.size() > 1 ? "s" : ""); | |
121 if (failed_tests.empty()) | |
122 return true; | |
123 | |
124 printf("Failing tests:\n"); | |
125 for (std::vector<std::string>::const_iterator iter = failed_tests.begin(); | |
126 iter != failed_tests.end(); ++iter) { | |
127 printf("%s\n", iter->c_str()); | |
128 } | |
129 | |
130 return false; | |
131 } | |
132 | |
133 } // namespace | |
OLD | NEW |