gtest_environment_test.cc 6.3 KB

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