unit_test_launcher.h 5.5 KB

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