googletest/googletest/test/gtest_repeat_test.cc
Go to the documentation of this file.
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 
30 
31 // Tests the --gtest_repeat=number flag.
32 
33 #include <stdlib.h>
34 #include <iostream>
35 #include "gtest/gtest.h"
36 #include "src/gtest-internal-inl.h"
37 
38 namespace {
39 
40 // We need this when we are testing Google Test itself and therefore
41 // cannot use Google Test assertions.
42 #define GTEST_CHECK_INT_EQ_(expected, actual) \
43  do {\
44  const int expected_val = (expected);\
45  const int actual_val = (actual);\
46  if (::testing::internal::IsTrue(expected_val != actual_val)) {\
47  ::std::cout << "Value of: " #actual "\n"\
48  << " Actual: " << actual_val << "\n"\
49  << "Expected: " #expected "\n"\
50  << "Which is: " << expected_val << "\n";\
51  ::testing::internal::posix::Abort();\
52  }\
53  } while (::testing::internal::AlwaysFalse())
54 
55 
56 // Used for verifying that global environment set-up and tear-down are
57 // inside the --gtest_repeat loop.
58 
59 int g_environment_set_up_count = 0;
60 int g_environment_tear_down_count = 0;
61 
62 class MyEnvironment : public testing::Environment {
63  public:
64  MyEnvironment() {}
65  void SetUp() override { g_environment_set_up_count++; }
66  void TearDown() override { g_environment_tear_down_count++; }
67 };
68 
69 // A test that should fail.
70 
71 int g_should_fail_count = 0;
72 
73 TEST(FooTest, ShouldFail) {
74  g_should_fail_count++;
75  EXPECT_EQ(0, 1) << "Expected failure.";
76 }
77 
78 // A test that should pass.
79 
80 int g_should_pass_count = 0;
81 
82 TEST(FooTest, ShouldPass) {
83  g_should_pass_count++;
84 }
85 
86 // A test that contains a thread-safe death test and a fast death
87 // test. It should pass.
88 
89 int g_death_test_count = 0;
90 
91 TEST(BarDeathTest, ThreadSafeAndFast) {
92  g_death_test_count++;
93 
94  GTEST_FLAG_SET(death_test_style, "threadsafe");
96 
97  GTEST_FLAG_SET(death_test_style, "fast");
99 }
100 
101 int g_param_test_count = 0;
102 
103 const int kNumberOfParamTests = 10;
104 
105 class MyParamTest : public testing::TestWithParam<int> {};
106 
107 TEST_P(MyParamTest, ShouldPass) {
108  GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
109  g_param_test_count++;
110 }
111 INSTANTIATE_TEST_SUITE_P(MyParamSequence,
112  MyParamTest,
113  testing::Range(0, kNumberOfParamTests));
114 
115 // Resets the count for each test.
116 void ResetCounts() {
117  g_environment_set_up_count = 0;
118  g_environment_tear_down_count = 0;
119  g_should_fail_count = 0;
120  g_should_pass_count = 0;
121  g_death_test_count = 0;
122  g_param_test_count = 0;
123 }
124 
125 // Checks that the count for each test is expected.
126 void CheckCounts(int expected) {
127  GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
128  GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
129  GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
130  GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
131  GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
132  GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
133 }
134 
135 // Tests the behavior of Google Test when --gtest_repeat is not specified.
136 void TestRepeatUnspecified() {
137  ResetCounts();
139  CheckCounts(1);
140 }
141 
142 // Tests the behavior of Google Test when --gtest_repeat has the given value.
143 void TestRepeat(int repeat) {
144  GTEST_FLAG_SET(repeat, repeat);
145  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
146 
147  ResetCounts();
148  GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
149  CheckCounts(repeat);
150 }
151 
152 // Tests using --gtest_repeat when --gtest_filter specifies an empty
153 // set of tests.
154 void TestRepeatWithEmptyFilter(int repeat) {
155  GTEST_FLAG_SET(repeat, repeat);
156  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
157  GTEST_FLAG_SET(filter, "None");
158 
159  ResetCounts();
161  CheckCounts(0);
162 }
163 
164 // Tests using --gtest_repeat when --gtest_filter specifies a set of
165 // successful tests.
166 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
167  GTEST_FLAG_SET(repeat, repeat);
168  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
169  GTEST_FLAG_SET(filter, "*-*ShouldFail");
170 
171  ResetCounts();
173  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
174  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
175  GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
176  GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
177  GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
178  GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
179 }
180 
181 // Tests using --gtest_repeat when --gtest_filter specifies a set of
182 // failed tests.
183 void TestRepeatWithFilterForFailedTests(int repeat) {
184  GTEST_FLAG_SET(repeat, repeat);
185  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
186  GTEST_FLAG_SET(filter, "*ShouldFail");
187 
188  ResetCounts();
190  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
191  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
192  GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
193  GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
194  GTEST_CHECK_INT_EQ_(0, g_death_test_count);
195  GTEST_CHECK_INT_EQ_(0, g_param_test_count);
196 }
197 
198 } // namespace
199 
200 int main(int argc, char **argv) {
201  testing::InitGoogleTest(&argc, argv);
202 
203  testing::AddGlobalTestEnvironment(new MyEnvironment);
204 
205  TestRepeatUnspecified();
206  TestRepeat(0);
207  TestRepeat(1);
208  TestRepeat(5);
209 
210  TestRepeatWithEmptyFilter(2);
211  TestRepeatWithEmptyFilter(3);
212 
213  TestRepeatWithFilterForSuccessfulTests(3);
214 
215  TestRepeatWithFilterForFailedTests(4);
216 
217  // It would be nice to verify that the tests indeed loop forever
218  // when GTEST_FLAG(repeat) is negative, but this test will be quite
219  // complicated to write. Since this flag is for interactive
220  // debugging only and doesn't affect the normal test result, such a
221  // test would be an overkill.
222 
223  printf("PASS\n");
224  return 0;
225 }
GTEST_FLAG_SET
#define GTEST_FLAG_SET(name, value)
Definition: googletest/googletest/include/gtest/internal/gtest-port.h:2219
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::Range
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:229
testing::AddGlobalTestEnvironment
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1474
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
TEST_P
#define TEST_P(test_suite_name, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:414
testing::internal::posix::Abort
void Abort()
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2076
testing::Environment
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1045
testing::Environment::SetUp
virtual void SetUp()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1051
testing::Environment::TearDown
virtual void TearDown()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1054
main
int main(int argc, char **argv)
Definition: googletest/googletest/test/gtest_repeat_test.cc:200
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
EXPECT_DEATH_IF_SUPPORTED
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-death-test.h:335
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
GTEST_CHECK_INT_EQ_
#define GTEST_CHECK_INT_EQ_(expected, actual)
Definition: googletest/googletest/test/gtest_repeat_test.cc:42
INSTANTIATE_TEST_SUITE_P
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name,...)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:460


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:55