test_suite.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2012 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_TEST_SUITE_H_
  5. #define BASE_TEST_TEST_SUITE_H_
  6. // Defines a basic test suite framework for running gtest based tests. You can
  7. // instantiate this class in your main function and call its Run method to run
  8. // any gtest based tests that are linked into your executable.
  9. #include <memory>
  10. #include "base/at_exit.h"
  11. #include "base/check.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/strings/string_piece.h"
  14. #include "base/tracing_buildflags.h"
  15. #include "build/build_config.h"
  16. #if BUILDFLAG(ENABLE_BASE_TRACING)
  17. #include "base/test/trace_to_file.h"
  18. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  19. namespace logging {
  20. class ScopedLogAssertHandler;
  21. }
  22. namespace testing {
  23. class TestInfo;
  24. }
  25. namespace base {
  26. class XmlUnitTestResultPrinter;
  27. // Instantiates TestSuite, runs it and returns exit code.
  28. int RunUnitTestsUsingBaseTestSuite(int argc, char** argv);
  29. class TestSuite {
  30. public:
  31. // Match function used by the GetTestCount method.
  32. typedef bool (*TestMatch)(const testing::TestInfo&);
  33. TestSuite(int argc, char** argv);
  34. #if BUILDFLAG(IS_WIN)
  35. TestSuite(int argc, wchar_t** argv);
  36. #endif // BUILDFLAG(IS_WIN)
  37. TestSuite(const TestSuite&) = delete;
  38. TestSuite& operator=(const TestSuite&) = delete;
  39. virtual ~TestSuite();
  40. int Run();
  41. // Disables checks for thread and process priority at the beginning and end of
  42. // each test. Most tests should not use this.
  43. void DisableCheckForThreadAndProcessPriority();
  44. // Disables checks for certain global objects being leaked across tests.
  45. void DisableCheckForLeakedGlobals();
  46. protected:
  47. // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
  48. // which gum up buildbots. Use a minimalistic assert handler which just
  49. // terminates the process.
  50. void UnitTestAssertHandler(const char* file,
  51. int line,
  52. const base::StringPiece summary,
  53. const base::StringPiece stack_trace);
  54. // Disable crash dialogs so that it doesn't gum up the buildbot
  55. virtual void SuppressErrorDialogs();
  56. // Override these for custom initialization and shutdown handling. Use these
  57. // instead of putting complex code in your constructor/destructor.
  58. virtual void Initialize();
  59. virtual void Shutdown();
  60. // Make sure that we setup an AtExitManager so Singleton objects will be
  61. // destroyed.
  62. std::unique_ptr<base::AtExitManager> at_exit_manager_;
  63. private:
  64. void AddTestLauncherResultPrinter();
  65. void InitializeFromCommandLine(int argc, char** argv);
  66. #if BUILDFLAG(IS_WIN)
  67. void InitializeFromCommandLine(int argc, wchar_t** argv);
  68. #endif // BUILDFLAG(IS_WIN)
  69. // Basic initialization for the test suite happens here.
  70. void PreInitialize();
  71. #if BUILDFLAG(ENABLE_BASE_TRACING)
  72. test::TraceToFile trace_to_file_;
  73. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  74. bool initialized_command_line_ = false;
  75. raw_ptr<XmlUnitTestResultPrinter> printer_ = nullptr;
  76. std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;
  77. bool check_for_leaked_globals_ = true;
  78. bool check_for_thread_and_process_priority_ = true;
  79. bool is_initialized_ = false;
  80. };
  81. } // namespace base
  82. #endif // BASE_TEST_TEST_SUITE_H_