From f45d5865ed0b2b8912244627cdf508a24cc6ccb4 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 24 Jan 2022 23:46:09 -0800 Subject: [PATCH] Make recreate_environments_when_repeating=false the default. So that global test environments are by default set up and torn down once, regardless of the value of the repeat flag. The point of global environments is to be set up and torn down once, and shared by all tests in the process. There is no obvious reason why multiple runs of the same test should be treated distinctly from single runs of different tests. Having this be false by default means that repeats using a global environment run faster. It can still be set to true if it's desired that every repeat get a fresh environment, but this seems less important given the nature of a global environment. Every test I've seen using a global environment uses it to set up some expensive external resource, not something that can/should be set up for each test anew. (Again this is unsurprising, since the environment is a global.) PiperOrigin-RevId: 424003937 Change-Id: I9e8a825cb8900960dd65b85fe5ffcc0a337e57f3 --- googletest/src/gtest.cc | 2 +- .../googletest-global-environment-unittest.py | 16 +++++++++------- googletest/test/googletest-listener-test.cc | 1 + googletest/test/gtest_repeat_test.cc | 4 ++++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index f8a291ae..04266dca 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -317,7 +317,7 @@ GTEST_DEFINE_int32_( GTEST_DEFINE_bool_( recreate_environments_when_repeating, testing::internal::BoolFromGTestEnv("recreate_environments_when_repeating", - true), + false), "Controls whether global test environments are recreated for each repeat " "of the tests. If set to false the global test environments are only set " "up once, for the first iteration, and only torn down once, for the last. " diff --git a/googletest/test/googletest-global-environment-unittest.py b/googletest/test/googletest-global-environment-unittest.py index ef2cfb85..26579344 100644 --- a/googletest/test/googletest-global-environment-unittest.py +++ b/googletest/test/googletest-global-environment-unittest.py @@ -71,10 +71,13 @@ class GTestGlobalEnvironmentUnitTest(gtest_test_utils.TestCase): def testEnvironmentSetUpAndTornDownForEachRepeat(self): """Tests the behavior of test environments and gtest_repeat.""" - txt = RunAndReturnOutput(['--gtest_repeat=2']) + # When --gtest_recreate_environments_when_repeating is true, the global test + # environment should be set up and torn down for each iteration. + txt = RunAndReturnOutput([ + '--gtest_repeat=2', + '--gtest_recreate_environments_when_repeating=true', + ]) - # By default, with gtest_repeat=2, the global test environment should be set - # up and torn down for each iteration. expected_pattern = ('(.|\n)*' r'Repeating all tests \(iteration 1\)' '(.|\n)*' @@ -97,13 +100,12 @@ class GTestGlobalEnvironmentUnitTest(gtest_test_utils.TestCase): def testEnvironmentSetUpAndTornDownOnce(self): """Tests environment and --gtest_recreate_environments_when_repeating.""" + # By default the environment should only be set up and torn down once, at + # the start and end of the test respectively. txt = RunAndReturnOutput([ - '--gtest_repeat=2', '--gtest_recreate_environments_when_repeating=false' + '--gtest_repeat=2', ]) - # When --gtest_recreate_environments_when_repeating is false, the test - # environment should only be set up and torn down once, at the start and - # end of the test respectively. expected_pattern = ('(.|\n)*' r'Repeating all tests \(iteration 1\)' '(.|\n)*' diff --git a/googletest/test/googletest-listener-test.cc b/googletest/test/googletest-listener-test.cc index 9d6c9cab..e7f9b13f 100644 --- a/googletest/test/googletest-listener-test.cc +++ b/googletest/test/googletest-listener-test.cc @@ -285,6 +285,7 @@ int main(int argc, char **argv) { << "AddGlobalTestEnvironment should not generate any events itself."; GTEST_FLAG_SET(repeat, 2); + GTEST_FLAG_SET(recreate_environments_when_repeating, true); int ret_val = RUN_ALL_TESTS(); #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ diff --git a/googletest/test/gtest_repeat_test.cc b/googletest/test/gtest_repeat_test.cc index c7af3efb..6b10048f 100644 --- a/googletest/test/gtest_repeat_test.cc +++ b/googletest/test/gtest_repeat_test.cc @@ -142,6 +142,7 @@ void TestRepeatUnspecified() { // Tests the behavior of Google Test when --gtest_repeat has the given value. void TestRepeat(int repeat) { GTEST_FLAG_SET(repeat, repeat); + GTEST_FLAG_SET(recreate_environments_when_repeating, true); ResetCounts(); GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS()); @@ -152,6 +153,7 @@ void TestRepeat(int repeat) { // set of tests. void TestRepeatWithEmptyFilter(int repeat) { GTEST_FLAG_SET(repeat, repeat); + GTEST_FLAG_SET(recreate_environments_when_repeating, true); GTEST_FLAG_SET(filter, "None"); ResetCounts(); @@ -163,6 +165,7 @@ void TestRepeatWithEmptyFilter(int repeat) { // successful tests. void TestRepeatWithFilterForSuccessfulTests(int repeat) { GTEST_FLAG_SET(repeat, repeat); + GTEST_FLAG_SET(recreate_environments_when_repeating, true); GTEST_FLAG_SET(filter, "*-*ShouldFail"); ResetCounts(); @@ -179,6 +182,7 @@ void TestRepeatWithFilterForSuccessfulTests(int repeat) { // failed tests. void TestRepeatWithFilterForFailedTests(int repeat) { GTEST_FLAG_SET(repeat, repeat); + GTEST_FLAG_SET(recreate_environments_when_repeating, true); GTEST_FLAG_SET(filter, "*ShouldFail"); ResetCounts();