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