gtest_environment_test.cc 6.3 KB

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