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 3 // found in the LICENSE file. 4 5 #ifndef BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 6 #define BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "base/callback.h" 14 #include "base/files/file_path.h" 15 #include "base/macros.h" 16 #include "base/test/launcher/test_launcher.h" 17 #include "build/build_config.h" 18 19 namespace base { 20 21 // Callback that runs a test suite and returns exit code. 22 using RunTestSuiteCallback = OnceCallback<int(void)>; 23 24 // Launches unit tests in given test suite. Returns exit code. 25 int LaunchUnitTests(int argc, char** argv, RunTestSuiteCallback run_test_suite); 26 27 // Same as above, but always runs tests serially. 28 int LaunchUnitTestsSerially(int argc, 29 char** argv, 30 RunTestSuiteCallback run_test_suite); 31 32 // Launches unit tests in given test suite. Returns exit code. 33 // |parallel_jobs| is the number of parallel test jobs. 34 // |default_batch_limit| is the default size of test batch 35 // (use 0 to disable batching). 36 // |use_job_objects| determines whether to use job objects. 37 int LaunchUnitTestsWithOptions(int argc, 38 char** argv, 39 size_t parallel_jobs, 40 int default_batch_limit, 41 bool use_job_objects, 42 RunTestSuiteCallback run_test_suite); 43 44 #if defined(OS_WIN) 45 // Launches unit tests in given test suite. Returns exit code. 46 // |use_job_objects| determines whether to use job objects. 47 int LaunchUnitTests(int argc, 48 wchar_t** argv, 49 bool use_job_objects, 50 RunTestSuiteCallback run_test_suite); 51 #endif // defined(OS_WIN) 52 53 // Delegate to abstract away platform differences for unit tests. 54 class UnitTestPlatformDelegate { 55 public: 56 // Called to get names of tests available for running. The delegate 57 // must put the result in |output| and return true on success. 58 virtual bool GetTests(std::vector<TestIdentifier>* output) = 0; 59 60 // Called to create a temporary for storing test results. The delegate 61 // must put the resulting path in |path| and return true on success. 62 virtual bool CreateResultsFile(base::FilePath* path) = 0; 63 64 // Called to create a new temporary file. The delegate must put the resulting 65 // path in |path| and return true on success. 66 virtual bool CreateTemporaryFile(base::FilePath* path) = 0; 67 68 // Returns command line for child GTest process based on the command line 69 // of current process. |test_names| is a vector of test full names 70 // (e.g. "A.B"), |output_file| is path to the GTest XML output file. 71 virtual CommandLine GetCommandLineForChildGTestProcess( 72 const std::vector<std::string>& test_names, 73 const base::FilePath& output_file, 74 const base::FilePath& flag_file) = 0; 75 76 // Returns wrapper to use for child GTest process. Empty string means 77 // no wrapper. 78 virtual std::string GetWrapperForChildGTestProcess() = 0; 79 80 // Relaunch tests, e.g. after a crash. 81 virtual void RelaunchTests(TestLauncher* test_launcher, 82 const std::vector<std::string>& test_names, 83 int launch_flags) = 0; 84 85 protected: 86 ~UnitTestPlatformDelegate() = default; 87 }; 88 89 // Runs tests serially, each in its own process. 90 void RunUnitTestsSerially(TestLauncher* test_launcher, 91 UnitTestPlatformDelegate* platform_delegate, 92 const std::vector<std::string>& test_names, 93 int launch_flags); 94 95 // Runs tests in batches (each batch in its own process). 96 void RunUnitTestsBatch(TestLauncher* test_launcher, 97 UnitTestPlatformDelegate* platform_delegate, 98 const std::vector<std::string>& test_names, 99 int launch_flags); 100 101 // Test launcher delegate for unit tests (mostly to support batching). 102 class UnitTestLauncherDelegate : public TestLauncherDelegate { 103 public: 104 UnitTestLauncherDelegate(UnitTestPlatformDelegate* delegate, 105 size_t batch_limit, 106 bool use_job_objects); 107 ~UnitTestLauncherDelegate() override; 108 109 private: 110 // TestLauncherDelegate: 111 bool GetTests(std::vector<TestIdentifier>* output) override; 112 bool ShouldRunTest(const std::string& test_case_name, 113 const std::string& test_name) override; 114 size_t RunTests(TestLauncher* test_launcher, 115 const std::vector<std::string>& test_names) override; 116 size_t RetryTests(TestLauncher* test_launcher, 117 const std::vector<std::string>& test_names) override; 118 119 ThreadChecker thread_checker_; 120 121 UnitTestPlatformDelegate* platform_delegate_; 122 123 // Maximum number of tests to run in a single batch. 124 size_t batch_limit_; 125 126 // Determines whether we use job objects on Windows. 127 bool use_job_objects_; 128 129 DISALLOW_COPY_AND_ASSIGN(UnitTestLauncherDelegate); 130 }; 131 132 } // namespace base 133 134 #endif // BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 135