1 // Copyright 2015 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.
5 #include "base/test/launcher/unit_test_launcher.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/test/gtest_util.h"
10 #include "base/test/gtest_xml_unittest_result_printer.h"
11 #include "base/test/test_switches.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 int LaunchUnitTests(int argc
,
18 const RunTestSuiteCallback
& run_test_suite
) {
19 CHECK(CommandLine::InitializedForCurrentProcess() ||
20 CommandLine::Init(argc
, argv
));
21 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
22 if (command_line
->HasSwitch(switches::kTestLauncherListTests
)) {
23 // Dump all test list into a file.
25 command_line
->GetSwitchValuePath(switches::kTestLauncherListTests
));
26 if (!WriteCompiledInTestsToFile(list_path
)) {
27 LOG(ERROR
) << "Failed to write list of tests.";
35 // Register XML output printer, if --test-launcher-output flag is set.
36 if (command_line
->HasSwitch(switches::kTestLauncherOutput
)) {
37 FilePath output_path
= command_line
->GetSwitchValuePath(
38 switches::kTestLauncherOutput
);
39 if (PathExists(output_path
)) {
40 LOG(WARNING
) << "Test launcher output path exists. Do not override";
42 XmlUnitTestResultPrinter
* printer
= new XmlUnitTestResultPrinter
;
43 CHECK(printer
->Initialize(output_path
));
44 testing::UnitTest::GetInstance()->listeners().Append(printer
);
48 return run_test_suite
.Run();