[Bug Sovle] : running a single DISABLED_ test does not call the environment TearDown if no SetUp is defined

This commit is contained in:
satasiyakrish1 2026-04-28 12:19:06 +05:30
parent b1906cf96e
commit d4774d6259
3 changed files with 49 additions and 3 deletions

View File

@ -64,3 +64,4 @@ Vadim Berman <vadimb@google.com>
Vlad Losev <vladl@google.com>
Wolfgang Klier <wklier@google.com>
Zhanyong Wan <wan@google.com>
Krish Satasiya <krishsatasiya44@gmail.com>

View File

@ -6003,6 +6003,21 @@ bool UnitTestImpl::RunAllTests() {
FilterTests(should_shard ? HONOR_SHARDING_PROTOCOL
: IGNORE_SHARDING_PROTOCOL) > 0;
// Check if any tests matched the filter, including disabled ones.
// This is needed to decide whether to run environment SetUp/TearDown:
// environments should be set up and torn down even if all matching tests
// are disabled, since the environment may need to perform cleanup.
bool has_tests_matching_filter = false;
for (const auto* test_suite : test_suites_) {
for (const TestInfo* test_info : test_suite->test_info_list()) {
if (test_info->matches_filter_ && !test_info->is_in_another_shard_) {
has_tests_matching_filter = true;
break;
}
}
if (has_tests_matching_filter) break;
}
// Lists the tests and exits if the --gtest_list_tests flag was specified.
if (GTEST_FLAG_GET(list_tests)) {
// This must be called *after* FilterTests() has been called.
@ -6056,8 +6071,12 @@ 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 global test environments if any tests matched
// the filter for this shard, including disabled tests. This ensures that
// environment TearDown() runs for cleanup even when all matching tests
// are disabled. Test suites are only actually executed when there are
// non-disabled tests to run (each TestSuite::Run() checks should_run_).
if (has_tests_matching_filter) {
// 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) {
@ -6113,7 +6132,9 @@ bool UnitTestImpl::RunAllTests() {
TearDownEnvironment);
repeater->OnEnvironmentsTearDownEnd(*parent_);
}
} else if (GTEST_FLAG_GET(fail_if_no_test_selected)) {
}
if (!has_tests_to_run && GTEST_FLAG_GET(fail_if_no_test_selected)) {
// If there were no tests to run, bail if we were requested to be
// strict.
constexpr char kNoTestsSelectedMessage[] =

View File

@ -86,6 +86,10 @@ class MyEnvironment : public testing::Environment {
// was run.
TEST(FooTest, Bar) { test_was_run = true; }
// A disabled test used to verify that environments are still set up and
// torn down when all matching tests are disabled.
TEST(FooTest, DISABLED_Baz) { FAIL() << "Disabled test should not run."; }
// Prints the message and aborts the program if condition is false.
void Check(bool condition, const char* msg) {
if (!condition) {
@ -158,6 +162,25 @@ void TestNoTestsRunSetUpFailure() {
"The global tear-down should run, as the global set-up was run.");
}
// Verifies that RUN_ALL_TESTS() runs the global set-up and tear-down
// even when all tests matching the filter are disabled.
void TestDisabledTestsRunEnvironments() {
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 should not have run.");
Check(set_up_was_run,
"The global set-up should run, even when all matching tests "
"are disabled.");
Check(tear_down_was_run,
"The global tear-down should run, even when all matching tests "
"are disabled.");
GTEST_FLAG_SET(filter, "*");
}
// Verifies that RUN_ALL_TESTS() doesn't do global set-up or
// tear-down when there is no test to run.
void TestNoTestsSkipsSetUp() {
@ -180,6 +203,7 @@ int main(int argc, char** argv) {
TestGlobalSetUp();
TestTestsRun();
TestNoTestsRunSetUpFailure();
TestDisabledTestsRunEnvironments();
TestNoTestsSkipsSetUp();
printf("PASS\n");