Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest_environment_test.cc
Go to the documentation of this file.
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 //
31 // Tests using global test environments.
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "gtest/gtest.h"
37 #include "src/gtest-internal-inl.h"
38 
39 namespace {
40 
41 enum FailureType { NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE };
42 
43 // Was SetUp run?
44 bool set_up_was_run;
45 // Was TearDown run?
46 bool tear_down_was_run;
47 // Was the TEST run?
48 bool test_was_run;
49 
50 // For testing using global test environments.
51 class MyEnvironment : public testing::Environment {
52  public:
53  // Depending on the value of failure_in_set_up_, SetUp() will
54  // generate a non-fatal failure, generate a fatal failure, or
55  // succeed.
56  void SetUp() override {
57  set_up_was_run = true;
58 
59  switch (failure_in_set_up_) {
60  case NON_FATAL_FAILURE:
61  ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
62  break;
63  case FATAL_FAILURE:
64  FAIL() << "Expected fatal failure in global set-up.";
65  break;
66  default:
67  break;
68  }
69  }
70 
71  // Generates a non-fatal failure.
72  void TearDown() override {
73  tear_down_was_run = true;
74  ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
75  }
76 
77  // We call this function to set the type of failure SetUp() should
78  // generate.
79  void set_failure_in_set_up(FailureType type) { failure_in_set_up_ = type; }
80 
81  private:
82  FailureType failure_in_set_up_;
83 };
84 
85 // The sole purpose of this TEST is to enable us to check whether it
86 // was run.
87 TEST(FooTest, Bar) { test_was_run = true; }
88 
89 // Prints the message and aborts the program if condition is false.
90 void Check(bool condition, const char* msg) {
91  if (!condition) {
92  printf("FAILED: %s\n", msg);
94  }
95 }
96 
97 // Runs the tests. Return true if and only if successful.
98 //
99 // The 'failure' parameter specifies the type of failure that should
100 // be generated by the global set-up.
101 int RunAllTests(MyEnvironment* env, FailureType failure) {
102  set_up_was_run = false;
103  tear_down_was_run = false;
104  test_was_run = false;
105  env->set_failure_in_set_up(failure);
107  return RUN_ALL_TESTS();
108 }
109 
110 // Registers a global test environment, and verifies that the
111 // registration function returns its argument.
112 MyEnvironment* RegisterTestEnv() {
113  MyEnvironment* const env = new MyEnvironment;
114  Check(testing::AddGlobalTestEnvironment(env) == env,
115  "AddGlobalTestEnvironment() should return its argument.");
116  return env;
117 }
118 
119 // Verifies that RUN_ALL_TESTS() runs the tests when the global
120 // set-up is successful.
121 void TestGlobalSetUp() {
122  MyEnvironment* const env = RegisterTestEnv();
123  Check(RunAllTests(env, NO_FAILURE) != 0,
124  "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
125  "should generate a failure.");
126  Check(test_was_run,
127  "The tests should run, as the global set-up should generate no "
128  "failure");
129  Check(tear_down_was_run,
130  "The global tear-down should run, as the global set-up was run.");
131 }
132 
133 // Verifies that RUN_ALL_TESTS() runs the tests when the global
134 // set-up generates no fatal failure.
135 void TestTestsRun() {
136  MyEnvironment* const env = RegisterTestEnv();
137  Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
138  "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
139  "and the global tear-down should generate a non-fatal failure.");
140  Check(test_was_run,
141  "The tests should run, as the global set-up should generate no "
142  "fatal failure.");
143  Check(tear_down_was_run,
144  "The global tear-down should run, as the global set-up was run.");
145 }
146 
147 // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
148 // generates a fatal failure.
149 void TestNoTestsRunSetUpFailure() {
150  MyEnvironment* const env = RegisterTestEnv();
151  Check(RunAllTests(env, FATAL_FAILURE) != 0,
152  "RUN_ALL_TESTS() should return non-zero, as the global set-up "
153  "should generate a fatal failure.");
154  Check(!test_was_run,
155  "The tests should not run, as the global set-up should generate "
156  "a fatal failure.");
157  Check(tear_down_was_run,
158  "The global tear-down should run, as the global set-up was run.");
159 }
160 
161 // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
162 // tear-down when there is no test to run.
163 void TestNoTestsSkipsSetUp() {
164  MyEnvironment* const env = RegisterTestEnv();
165  GTEST_FLAG_SET(filter, "-*");
166  Check(RunAllTests(env, NO_FAILURE) == 0,
167  "RUN_ALL_TESTS() should return zero, as there is no test to run.");
168  Check(!set_up_was_run,
169  "The global set-up should not run, as there is no test to run.");
170  Check(!tear_down_was_run,
171  "The global tear-down should not run, "
172  "as the global set-up was not run.");
173 }
174 
175 } // namespace
176 
177 int main(int argc, char** argv) {
178  testing::InitGoogleTest(&argc, argv);
179 
180  TestGlobalSetUp();
181  TestTestsRun();
182  TestNoTestsRunSetUpFailure();
183  TestNoTestsSkipsSetUp();
184 
185  printf("PASS\n");
186  return 0;
187 }
class UnitTestImpl * GetUnitTestImpl()
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1345
virtual void SetUp()
Definition: gtest.h:899
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2192
int main()
Definition: ad_example.cpp:171
#define GTEST_FLAG_SET(name, value)
Definition: gtest-port.h:2343
int RunAllTests()
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2334
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6885
#define ADD_FAILURE()
Definition: gtest.h:1750
#define FAIL()
Definition: gtest.h:1769
virtual void TearDown()
Definition: gtest.h:902