diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 16:43:27 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 16:43:27 +0000 |
commit | 42a116127704f97231765ff599cc1267747e2a70 (patch) | |
tree | 13f05cd5a09b75a2dd7649f12ddd0399b837beb4 /chrome/test/test_launcher/test_runner.cc | |
parent | 02528ba2437c46f8ced345431f8ee9ec25625577 (diff) | |
download | chromium_src-42a116127704f97231765ff599cc1267747e2a70.zip chromium_src-42a116127704f97231765ff599cc1267747e2a70.tar.gz chromium_src-42a116127704f97231765ff599cc1267747e2a70.tar.bz2 |
This CL makes a more generic browser test launcher and a DLL of the
interactive UI tests, so the interactive UI tests can be run isolated.
BUG=None
TEST=Run the the interactive ui tests with the new launcher:
test_launcher --lib=interactive_ui_tests_dll
Review URL: http://codereview.chromium.org/197045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25863 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/test_launcher/test_runner.cc')
-rw-r--r-- | chrome/test/test_launcher/test_runner.cc | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/chrome/test/test_launcher/test_runner.cc b/chrome/test/test_launcher/test_runner.cc new file mode 100644 index 0000000..4fb34cd --- /dev/null +++ b/chrome/test/test_launcher/test_runner.cc @@ -0,0 +1,132 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/test_launcher/test_runner.h" + +#include <vector> + +#include "base/command_line.h" +#include "base/logging.h" +#include "base/process_util.h" +#include "base/scoped_ptr.h" +#include "base/string_util.h" + +namespace { + +const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; +const wchar_t* const kGTestRunDisabledTestsFlag = + L"gtest_also_run_disabled_tests"; + +// Retrieves the list of tests to run by running gtest with the +// --gtest_list_tests flag in a forked process and parsing its output. +// |command_line| should contain the command line used to start the browser +// test launcher, it is expected that it does not contain the +// --gtest_list_tests flag already. +// Note: we cannot implement this in-process for InProcessTestRunner as GTest +// prints to the stdout and there are no good way of temporarily redirecting +// outputs. +bool GetTestList(const CommandLine& command_line, + std::vector<std::string>* test_list) { + DCHECK(!command_line.HasSwitch(kGTestListTestsFlag)); + + // Run ourselves with the --gtest_list_tests option and read the output. + CommandLine new_command_line(command_line); + new_command_line.AppendSwitch(kGTestListTestsFlag); + std::string output; + if (!base::GetAppOutput(new_command_line, &output)) + return false; + + // The output looks like: + // TestCase. + // Test1 + // Test2 + // OtherTestCase. + // FooTest + // ... + std::vector<std::string> lines; + SplitString(output, '\n', &lines); + + std::string test_case; + for (std::vector<std::string>::const_iterator iter = lines.begin(); + iter != lines.end(); ++iter) { + std::string line = *iter; + if (line.empty()) + continue; // Just ignore empty lines if any. + + if (line[line.size() - 1] == '.') { + // This is a new test case. + test_case = line; + continue; + } + + if (!command_line.HasSwitch(kGTestRunDisabledTestsFlag) && + line.find("DISABLED") != std::string::npos) + continue; // Skip disabled tests. + + // We are dealing with a test. + test_list->push_back(test_case + line); + } + return true; +} + +} // namespace + +namespace tests { + +TestRunner::TestRunner() { +} + +TestRunner::~TestRunner() { +} + +bool RunTests(const TestRunnerFactory& test_runner_factory) { + const CommandLine* command_line = CommandLine::ForCurrentProcess(); + + DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); + + // First let's get the list of tests we need to run. + std::vector<std::string> test_list; + if (!GetTestList(*command_line, &test_list)) { + printf("Failed to retrieve the tests to run.\n"); + return false; + } + + if (test_list.empty()) { + printf("No tests to run.\n"); + return false; + } + + int test_run_count = 0; + std::vector<std::string> failed_tests; + for (std::vector<std::string>::const_iterator iter = test_list.begin(); + iter != test_list.end(); ++iter) { + std::string test_name = *iter; + scoped_ptr<TestRunner> test_runner(test_runner_factory.CreateTestRunner()); + if (!test_runner.get() || !test_runner->Init()) + return false; + test_run_count++; + if (!test_runner->RunTest(test_name.c_str())) { + if (std::find(failed_tests.begin(), failed_tests.end(), test_name) == + failed_tests.end()) { + failed_tests.push_back(*iter); + } + } + } + + printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); + printf("%d test%s failed\n", static_cast<int>(failed_tests.size()), + failed_tests.size() > 1 ? "s" : ""); + if (failed_tests.empty()) + return true; + + printf("Failing tests:\n"); + for (std::vector<std::string>::const_iterator iter = failed_tests.begin(); + iter != failed_tests.end(); ++iter) { + printf("%s\n", iter->c_str()); + } + + return false; +} + +} // namespace |