gtest_repeat_test.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2008, 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. // Tests the --gtest_repeat=number flag.
  30. #include <stdlib.h>
  31. #include <iostream>
  32. #include "gtest/gtest.h"
  33. #include "src/gtest-internal-inl.h"
  34. namespace {
  35. // We need this when we are testing Google Test itself and therefore
  36. // cannot use Google Test assertions.
  37. #define GTEST_CHECK_INT_EQ_(expected, actual) \
  38. do { \
  39. const int expected_val = (expected); \
  40. const int actual_val = (actual); \
  41. if (::testing::internal::IsTrue(expected_val != actual_val)) { \
  42. ::std::cout << "Value of: " #actual "\n" \
  43. << " Actual: " << actual_val << "\n" \
  44. << "Expected: " #expected "\n" \
  45. << "Which is: " << expected_val << "\n"; \
  46. ::testing::internal::posix::Abort(); \
  47. } \
  48. } while (::testing::internal::AlwaysFalse())
  49. // Used for verifying that global environment set-up and tear-down are
  50. // inside the --gtest_repeat loop.
  51. int g_environment_set_up_count = 0;
  52. int g_environment_tear_down_count = 0;
  53. class MyEnvironment : public testing::Environment {
  54. public:
  55. MyEnvironment() {}
  56. void SetUp() override { g_environment_set_up_count++; }
  57. void TearDown() override { g_environment_tear_down_count++; }
  58. };
  59. // A test that should fail.
  60. int g_should_fail_count = 0;
  61. TEST(FooTest, ShouldFail) {
  62. g_should_fail_count++;
  63. EXPECT_EQ(0, 1) << "Expected failure.";
  64. }
  65. // A test that should pass.
  66. int g_should_pass_count = 0;
  67. TEST(FooTest, ShouldPass) { g_should_pass_count++; }
  68. // A test that contains a thread-safe death test and a fast death
  69. // test. It should pass.
  70. int g_death_test_count = 0;
  71. TEST(BarDeathTest, ThreadSafeAndFast) {
  72. g_death_test_count++;
  73. GTEST_FLAG_SET(death_test_style, "threadsafe");
  74. EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
  75. GTEST_FLAG_SET(death_test_style, "fast");
  76. EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
  77. }
  78. int g_param_test_count = 0;
  79. const int kNumberOfParamTests = 10;
  80. class MyParamTest : public testing::TestWithParam<int> {};
  81. TEST_P(MyParamTest, ShouldPass) {
  82. GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
  83. g_param_test_count++;
  84. }
  85. INSTANTIATE_TEST_SUITE_P(MyParamSequence, MyParamTest,
  86. testing::Range(0, kNumberOfParamTests));
  87. // Resets the count for each test.
  88. void ResetCounts() {
  89. g_environment_set_up_count = 0;
  90. g_environment_tear_down_count = 0;
  91. g_should_fail_count = 0;
  92. g_should_pass_count = 0;
  93. g_death_test_count = 0;
  94. g_param_test_count = 0;
  95. }
  96. // Checks that the count for each test is expected.
  97. void CheckCounts(int expected) {
  98. GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
  99. GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
  100. GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
  101. GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
  102. GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
  103. GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
  104. }
  105. // Tests the behavior of Google Test when --gtest_repeat is not specified.
  106. void TestRepeatUnspecified() {
  107. ResetCounts();
  108. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  109. CheckCounts(1);
  110. }
  111. // Tests the behavior of Google Test when --gtest_repeat has the given value.
  112. void TestRepeat(int repeat) {
  113. GTEST_FLAG_SET(repeat, repeat);
  114. GTEST_FLAG_SET(recreate_environments_when_repeating, true);
  115. ResetCounts();
  116. GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
  117. CheckCounts(repeat);
  118. }
  119. // Tests using --gtest_repeat when --gtest_filter specifies an empty
  120. // set of tests.
  121. void TestRepeatWithEmptyFilter(int repeat) {
  122. GTEST_FLAG_SET(repeat, repeat);
  123. GTEST_FLAG_SET(recreate_environments_when_repeating, true);
  124. GTEST_FLAG_SET(filter, "None");
  125. ResetCounts();
  126. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  127. CheckCounts(0);
  128. }
  129. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  130. // successful tests.
  131. void TestRepeatWithFilterForSuccessfulTests(int repeat) {
  132. GTEST_FLAG_SET(repeat, repeat);
  133. GTEST_FLAG_SET(recreate_environments_when_repeating, true);
  134. GTEST_FLAG_SET(filter, "*-*ShouldFail");
  135. ResetCounts();
  136. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  137. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  138. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  139. GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
  140. GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
  141. GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
  142. GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
  143. }
  144. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  145. // failed tests.
  146. void TestRepeatWithFilterForFailedTests(int repeat) {
  147. GTEST_FLAG_SET(repeat, repeat);
  148. GTEST_FLAG_SET(recreate_environments_when_repeating, true);
  149. GTEST_FLAG_SET(filter, "*ShouldFail");
  150. ResetCounts();
  151. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  152. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  153. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  154. GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
  155. GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
  156. GTEST_CHECK_INT_EQ_(0, g_death_test_count);
  157. GTEST_CHECK_INT_EQ_(0, g_param_test_count);
  158. }
  159. } // namespace
  160. int main(int argc, char **argv) {
  161. testing::InitGoogleTest(&argc, argv);
  162. testing::AddGlobalTestEnvironment(new MyEnvironment);
  163. TestRepeatUnspecified();
  164. TestRepeat(0);
  165. TestRepeat(1);
  166. TestRepeat(5);
  167. TestRepeatWithEmptyFilter(2);
  168. TestRepeatWithEmptyFilter(3);
  169. TestRepeatWithFilterForSuccessfulTests(3);
  170. TestRepeatWithFilterForFailedTests(4);
  171. // It would be nice to verify that the tests indeed loop forever
  172. // when GTEST_FLAG(repeat) is negative, but this test will be quite
  173. // complicated to write. Since this flag is for interactive
  174. // debugging only and doesn't affect the normal test result, such a
  175. // test would be an overkill.
  176. printf("PASS\n");
  177. return 0;
  178. }