From a6d3656c49a74af4ea1794ebd0d6d62387be2b8a Mon Sep 17 00:00:00 2001 From: KirtiRamchandani Date: Mon, 1 Jun 2026 23:00:01 +0530 Subject: [PATCH] Run environments for selected disabled tests --- googletest/src/gtest.cc | 9 +++++++-- googletest/test/gtest_environment_test.cc | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index ac90786a0..53f7b3373 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -6002,6 +6002,8 @@ bool UnitTestImpl::RunAllTests() { const bool has_tests_to_run = FilterTests(should_shard ? HONOR_SHARDING_PROTOCOL : IGNORE_SHARDING_PROTOCOL) > 0; + const bool has_tests_selected = + has_tests_to_run || reportable_disabled_test_count() > 0; // Lists the tests and exits if the --gtest_list_tests flag was specified. if (GTEST_FLAG_GET(list_tests)) { @@ -6056,8 +6058,11 @@ bool UnitTestImpl::RunAllTests() { // Tells the unit test event listeners that the tests are about to start. repeater->OnTestIterationStart(*parent_, i); - // Runs each test suite if there is at least one test to run. - if (has_tests_to_run) { + // Sets up and tears down environments if there is at least one test to run, + // or if the selected tests are all disabled. The disabled tests still do + // not run, but global tear-down can clean resources created before + // RUN_ALL_TESTS(). + if (has_tests_selected) { // Sets up all environments beforehand. If test environments aren't // recreated for each iteration, only do so on the first iteration. if (i == 0 || recreate_environments_when_repeating) { diff --git a/googletest/test/gtest_environment_test.cc b/googletest/test/gtest_environment_test.cc index 03657c7d8..383701675 100644 --- a/googletest/test/gtest_environment_test.cc +++ b/googletest/test/gtest_environment_test.cc @@ -86,6 +86,8 @@ class MyEnvironment : public testing::Environment { // was run. TEST(FooTest, Bar) { test_was_run = true; } +TEST(FooTest, DISABLED_Baz) { test_was_run = true; } + // Prints the message and aborts the program if condition is false. void Check(bool condition, const char* msg) { if (!condition) { @@ -172,6 +174,21 @@ void TestNoTestsSkipsSetUp() { "as the global set-up was not run."); } +// Verifies that RUN_ALL_TESTS() still runs environment tear-down when the +// selected tests are disabled. +void TestDisabledTestsRunEnvironmentTearDown() { + MyEnvironment* const env = RegisterTestEnv(); + GTEST_FLAG_SET(filter, "FooTest.DISABLED_Baz"); + Check(RunAllTests(env, NO_FAILURE) != 0, + "RUN_ALL_TESTS() should return non-zero, as the global tear-down " + "should generate a failure."); + Check(!test_was_run, "The disabled test body should not run."); + Check(set_up_was_run, + "The global set-up should run, as a disabled test was selected."); + Check(tear_down_was_run, + "The global tear-down should run, as the global set-up was run."); +} + } // namespace int main(int argc, char** argv) { @@ -181,6 +198,7 @@ int main(int argc, char** argv) { TestTestsRun(); TestNoTestsRunSetUpFailure(); TestNoTestsSkipsSetUp(); + TestDisabledTestsRunEnvironmentTearDown(); printf("PASS\n"); return 0;