41 enum FailureType { NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE };
46 bool tear_down_was_run;
56 void SetUp()
override {
57 set_up_was_run =
true;
59 switch (failure_in_set_up_) {
60 case NON_FATAL_FAILURE:
61 ADD_FAILURE() <<
"Expected non-fatal failure in global set-up.";
64 FAIL() <<
"Expected fatal failure in global set-up.";
73 tear_down_was_run =
true;
74 ADD_FAILURE() <<
"Expected non-fatal failure in global tear-down.";
79 void set_failure_in_set_up(
FailureType type) { failure_in_set_up_ = type; }
87 TEST(FooTest, Bar) { test_was_run =
true; }
90 void Check(
bool condition,
const char* msg) {
92 printf(
"FAILED: %s\n", msg);
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);
112 MyEnvironment* RegisterTestEnv() {
113 MyEnvironment*
const env =
new MyEnvironment;
115 "AddGlobalTestEnvironment() should return its argument.");
121 void TestGlobalSetUp() {
122 MyEnvironment*
const env = RegisterTestEnv();
124 "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
125 "should generate a failure.");
127 "The tests should run, as the global set-up should generate no "
129 Check(tear_down_was_run,
130 "The global tear-down should run, as the global set-up was run.");
135 void TestTestsRun() {
136 MyEnvironment*
const env = RegisterTestEnv();
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.");
141 "The tests should run, as the global set-up should generate no "
143 Check(tear_down_was_run,
144 "The global tear-down should run, as the global set-up was run.");
149 void TestNoTestsRunSetUpFailure() {
150 MyEnvironment*
const env = RegisterTestEnv();
152 "RUN_ALL_TESTS() should return non-zero, as the global set-up "
153 "should generate a fatal failure.");
155 "The tests should not run, as the global set-up should generate "
157 Check(tear_down_was_run,
158 "The global tear-down should run, as the global set-up was run.");
163 void TestNoTestsSkipsSetUp() {
164 MyEnvironment*
const env = RegisterTestEnv();
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.");
177 int main(
int argc,
char** argv) {
182 TestNoTestsRunSetUpFailure();
183 TestNoTestsSkipsSetUp();
class UnitTestImpl * GetUnitTestImpl()
Environment * AddGlobalTestEnvironment(Environment *env)
void ClearAdHocTestResult()
#define TEST(test_suite_name, test_name)
#define GTEST_FLAG_SET(name, value)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
GTEST_API_ void InitGoogleTest(int *argc, char **argv)