test_suite.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <string>
  11. #include "base/at_exit.h"
  12. #include "base/check.h"
  13. #include "base/macros.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 defined(OS_WIN)
  35. TestSuite(int argc, wchar_t** argv);
  36. #endif // defined(OS_WIN)
  37. virtual ~TestSuite();
  38. int Run();
  39. // Disables checks for thread and process priority at the beginning and end of
  40. // each test. Most tests should not use this.
  41. void DisableCheckForThreadAndProcessPriority();
  42. // Disables checks for certain global objects being leaked across tests.
  43. void DisableCheckForLeakedGlobals();
  44. protected:
  45. // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
  46. // which gum up buildbots. Use a minimalistic assert handler which just
  47. // terminates the process.
  48. void UnitTestAssertHandler(const char* file,
  49. int line,
  50. const base::StringPiece summary,
  51. const base::StringPiece stack_trace);
  52. // Disable crash dialogs so that it doesn't gum up the buildbot
  53. virtual void SuppressErrorDialogs();
  54. // Override these for custom initialization and shutdown handling. Use these
  55. // instead of putting complex code in your constructor/destructor.
  56. virtual void Initialize();
  57. virtual void Shutdown();
  58. // Make sure that we setup an AtExitManager so Singleton objects will be
  59. // destroyed.
  60. std::unique_ptr<base::AtExitManager> at_exit_manager_;
  61. private:
  62. void AddTestLauncherResultPrinter();
  63. void InitializeFromCommandLine(int argc, char** argv);
  64. #if defined(OS_WIN)
  65. void InitializeFromCommandLine(int argc, wchar_t** argv);
  66. #endif // defined(OS_WIN)
  67. // Basic initialization for the test suite happens here.
  68. void PreInitialize();
  69. #if BUILDFLAG(ENABLE_BASE_TRACING)
  70. test::TraceToFile trace_to_file_;
  71. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  72. bool initialized_command_line_ = false;
  73. XmlUnitTestResultPrinter* printer_ = nullptr;
  74. std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;
  75. bool check_for_leaked_globals_ = true;
  76. bool check_for_thread_and_process_priority_ = true;
  77. bool is_initialized_ = false;
  78. DISALLOW_COPY_AND_ASSIGN(TestSuite);
  79. };
  80. } // namespace base
  81. #endif // BASE_TEST_TEST_SUITE_H_