gtest_environment_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Tests using global test environments.
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include "gtest/gtest.h"
  36. #define GTEST_IMPLEMENTATION_ 1 // Required for the next #include.
  37. #include "src/gtest-internal-inl.h"
  38. #undef GTEST_IMPLEMENTATION_
  39. namespace testing {
  40. GTEST_DECLARE_string_(filter);
  41. }
  42. namespace {
  43. enum FailureType {
  44. NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
  45. };
  46. // For testing using global test environments.
  47. class MyEnvironment : public testing::Environment {
  48. public:
  49. MyEnvironment() { Reset(); }
  50. // Depending on the value of failure_in_set_up_, SetUp() will
  51. // generate a non-fatal failure, generate a fatal failure, or
  52. // succeed.
  53. virtual void SetUp() {
  54. set_up_was_run_ = true;
  55. switch (failure_in_set_up_) {
  56. case NON_FATAL_FAILURE:
  57. ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
  58. break;
  59. case FATAL_FAILURE:
  60. FAIL() << "Expected fatal failure in global set-up.";
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. // Generates a non-fatal failure.
  67. virtual void TearDown() {
  68. tear_down_was_run_ = true;
  69. ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
  70. }
  71. // Resets the state of the environment s.t. it can be reused.
  72. void Reset() {
  73. failure_in_set_up_ = NO_FAILURE;
  74. set_up_was_run_ = false;
  75. tear_down_was_run_ = false;
  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) {
  80. failure_in_set_up_ = type;
  81. }
  82. // Was SetUp() run?
  83. bool set_up_was_run() const { return set_up_was_run_; }
  84. // Was TearDown() run?
  85. bool tear_down_was_run() const { return tear_down_was_run_; }
  86. private:
  87. FailureType failure_in_set_up_;
  88. bool set_up_was_run_;
  89. bool tear_down_was_run_;
  90. };
  91. // Was the TEST run?
  92. bool test_was_run;
  93. // The sole purpose of this TEST is to enable us to check whether it
  94. // was run.
  95. TEST(FooTest, Bar) {
  96. test_was_run = true;
  97. }
  98. // Prints the message and aborts the program if condition is false.
  99. void Check(bool condition, const char* msg) {
  100. if (!condition) {
  101. printf("FAILED: %s\n", msg);
  102. testing::internal::posix::Abort();
  103. }
  104. }
  105. // Runs the tests. Return true iff successful.
  106. //
  107. // The 'failure' parameter specifies the type of failure that should
  108. // be generated by the global set-up.
  109. int RunAllTests(MyEnvironment* env, FailureType failure) {
  110. env->Reset();
  111. env->set_failure_in_set_up(failure);
  112. test_was_run = false;
  113. testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
  114. return RUN_ALL_TESTS();
  115. }
  116. } // namespace
  117. int main(int argc, char **argv) {
  118. testing::InitGoogleTest(&argc, argv);
  119. // Registers a global test environment, and verifies that the
  120. // registration function returns its argument.
  121. MyEnvironment* const env = new MyEnvironment;
  122. Check(testing::AddGlobalTestEnvironment(env) == env,
  123. "AddGlobalTestEnvironment() should return its argument.");
  124. // Verifies that RUN_ALL_TESTS() runs the tests when the global
  125. // set-up is successful.
  126. Check(RunAllTests(env, NO_FAILURE) != 0,
  127. "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
  128. "should generate a failure.");
  129. Check(test_was_run,
  130. "The tests should run, as the global set-up should generate no "
  131. "failure");
  132. Check(env->tear_down_was_run(),
  133. "The global tear-down should run, as the global set-up was run.");
  134. // Verifies that RUN_ALL_TESTS() runs the tests when the global
  135. // set-up generates no fatal failure.
  136. Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
  137. "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
  138. "and the global tear-down should generate a non-fatal failure.");
  139. Check(test_was_run,
  140. "The tests should run, as the global set-up should generate no "
  141. "fatal failure.");
  142. Check(env->tear_down_was_run(),
  143. "The global tear-down should run, as the global set-up was run.");
  144. // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
  145. // generates a fatal failure.
  146. Check(RunAllTests(env, FATAL_FAILURE) != 0,
  147. "RUN_ALL_TESTS() should return non-zero, as the global set-up "
  148. "should generate a fatal failure.");
  149. Check(!test_was_run,
  150. "The tests should not run, as the global set-up should generate "
  151. "a fatal failure.");
  152. Check(env->tear_down_was_run(),
  153. "The global tear-down should run, as the global set-up was run.");
  154. // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
  155. // tear-down when there is no test to run.
  156. testing::GTEST_FLAG(filter) = "-*";
  157. Check(RunAllTests(env, NO_FAILURE) == 0,
  158. "RUN_ALL_TESTS() should return zero, as there is no test to run.");
  159. Check(!env->set_up_was_run(),
  160. "The global set-up should not run, as there is no test to run.");
  161. Check(!env->tear_down_was_run(),
  162. "The global tear-down should not run, "
  163. "as the global set-up was not run.");
  164. printf("PASS\n");
  165. return 0;
  166. }