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
This commit is contained in:
Abseil Team 2022-01-24 23:46:09 -08:00 committed by Copybara-Service
parent 7eae8de0da
commit f45d5865ed
4 changed files with 15 additions and 8 deletions

View File

@ -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. "

View File

@ -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)*'

View File

@ -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_

View File

@ -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();