gtest_repeat_test.cc 7.2 KB

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