• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_TEST_TEST_SUITE_H_
6 #define BASE_TEST_TEST_SUITE_H_
7 
8 // Defines a basic test suite framework for running gtest based tests.  You can
9 // instantiate this class in your main function and call its Run method to run
10 // any gtest based tests that are linked into your executable.
11 
12 #include <memory>
13 #include <string>
14 
15 #include "base/at_exit.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/test/scoped_feature_list.h"
19 #include "base/test/trace_to_file.h"
20 #include "build/build_config.h"
21 
22 namespace testing {
23 class TestInfo;
24 }
25 
26 namespace base {
27 
28 class XmlUnitTestResultPrinter;
29 
30 // Instantiates TestSuite, runs it and returns exit code.
31 int RunUnitTestsUsingBaseTestSuite(int argc, char **argv);
32 
33 class TestSuite {
34  public:
35   // Match function used by the GetTestCount method.
36   typedef bool (*TestMatch)(const testing::TestInfo&);
37 
38   TestSuite(int argc, char** argv);
39 #if defined(OS_WIN)
40   TestSuite(int argc, wchar_t** argv);
41 #endif  // defined(OS_WIN)
42   virtual ~TestSuite();
43 
44   // Returns true if the test is marked as "MAYBE_".
45   // When using different prefixes depending on platform, we use MAYBE_ and
46   // preprocessor directives to replace MAYBE_ with the target prefix.
47   static bool IsMarkedMaybe(const testing::TestInfo& test);
48 
49   void CatchMaybeTests();
50 
51   void ResetCommandLine();
52 
53   void AddTestLauncherResultPrinter();
54 
55   int Run();
56 
57  protected:
58   // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
59   // which gum up buildbots. Use a minimalistic assert handler which just
60   // terminates the process.
61   void UnitTestAssertHandler(const char* file,
62                              int line,
63                              const base::StringPiece summary,
64                              const base::StringPiece stack_trace);
65 
66   // Disable crash dialogs so that it doesn't gum up the buildbot
67   virtual void SuppressErrorDialogs();
68 
69   // Override these for custom initialization and shutdown handling.  Use these
70   // instead of putting complex code in your constructor/destructor.
71 
72   virtual void Initialize();
73   virtual void Shutdown();
74 
75   // Make sure that we setup an AtExitManager so Singleton objects will be
76   // destroyed.
77   std::unique_ptr<base::AtExitManager> at_exit_manager_;
78 
79  private:
80   void InitializeFromCommandLine(int argc, char** argv);
81 #if defined(OS_WIN)
82   void InitializeFromCommandLine(int argc, wchar_t** argv);
83 #endif  // defined(OS_WIN)
84 
85   // Basic initialization for the test suite happens here.
86   void PreInitialize();
87 
88   test::TraceToFile trace_to_file_;
89 
90   bool initialized_command_line_;
91 
92   test::ScopedFeatureList scoped_feature_list_;
93 
94   XmlUnitTestResultPrinter* printer_ = nullptr;
95 
96   std::unique_ptr<logging::ScopedLogAssertHandler> assert_handler_;
97 
98   DISALLOW_COPY_AND_ASSIGN(TestSuite);
99 };
100 
101 }  // namespace base
102 
103 #endif  // BASE_TEST_TEST_SUITE_H_
104