gtest_repeat_test.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. virtual void SetUp() { g_environment_set_up_count++; }
  65. virtual void TearDown() { 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. // FIXME: Make parameter value checking robust WRT order of tests.
  93. GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
  94. g_param_test_count++;
  95. }
  96. INSTANTIATE_TEST_CASE_P(MyParamSequence,
  97. MyParamTest,
  98. testing::Range(0, kNumberOfParamTests));
  99. // Resets the count for each test.
  100. void ResetCounts() {
  101. g_environment_set_up_count = 0;
  102. g_environment_tear_down_count = 0;
  103. g_should_fail_count = 0;
  104. g_should_pass_count = 0;
  105. g_death_test_count = 0;
  106. g_param_test_count = 0;
  107. }
  108. // Checks that the count for each test is expected.
  109. void CheckCounts(int expected) {
  110. GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
  111. GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
  112. GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
  113. GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
  114. GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
  115. GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
  116. }
  117. // Tests the behavior of Google Test when --gtest_repeat is not specified.
  118. void TestRepeatUnspecified() {
  119. ResetCounts();
  120. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  121. CheckCounts(1);
  122. }
  123. // Tests the behavior of Google Test when --gtest_repeat has the given value.
  124. void TestRepeat(int repeat) {
  125. GTEST_FLAG(repeat) = repeat;
  126. ResetCounts();
  127. GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
  128. CheckCounts(repeat);
  129. }
  130. // Tests using --gtest_repeat when --gtest_filter specifies an empty
  131. // set of tests.
  132. void TestRepeatWithEmptyFilter(int repeat) {
  133. GTEST_FLAG(repeat) = repeat;
  134. GTEST_FLAG(filter) = "None";
  135. ResetCounts();
  136. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  137. CheckCounts(0);
  138. }
  139. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  140. // successful tests.
  141. void TestRepeatWithFilterForSuccessfulTests(int repeat) {
  142. GTEST_FLAG(repeat) = repeat;
  143. GTEST_FLAG(filter) = "*-*ShouldFail";
  144. ResetCounts();
  145. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  146. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  147. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  148. GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
  149. GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
  150. GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
  151. GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
  152. }
  153. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  154. // failed tests.
  155. void TestRepeatWithFilterForFailedTests(int repeat) {
  156. GTEST_FLAG(repeat) = repeat;
  157. GTEST_FLAG(filter) = "*ShouldFail";
  158. ResetCounts();
  159. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  160. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  161. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  162. GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
  163. GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
  164. GTEST_CHECK_INT_EQ_(0, g_death_test_count);
  165. GTEST_CHECK_INT_EQ_(0, g_param_test_count);
  166. }
  167. } // namespace
  168. int main(int argc, char **argv) {
  169. testing::InitGoogleTest(&argc, argv);
  170. testing::AddGlobalTestEnvironment(new MyEnvironment);
  171. TestRepeatUnspecified();
  172. TestRepeat(0);
  173. TestRepeat(1);
  174. TestRepeat(5);
  175. TestRepeatWithEmptyFilter(2);
  176. TestRepeatWithEmptyFilter(3);
  177. TestRepeatWithFilterForSuccessfulTests(3);
  178. TestRepeatWithFilterForFailedTests(4);
  179. // It would be nice to verify that the tests indeed loop forever
  180. // when GTEST_FLAG(repeat) is negative, but this test will be quite
  181. // complicated to write. Since this flag is for interactive
  182. // debugging only and doesn't affect the normal test result, such a
  183. // test would be an overkill.
  184. printf("PASS\n");
  185. return 0;
  186. }