Skip to content

Commit 28e9d1f

Browse files
pizzudcopybara-github
authored andcommitted
googletest: Add a flag to fail if no tests were selected to run.
There are two cases that prompt this behavior: - All test cases in the binary are disabled. - There are more shards defined than test cases, so some shards are empty. The result in each case is the same: the test runner needs to spin up additional processes that do nothing, which is wasteful, especially when tests need expensive resources. PiperOrigin-RevId: 769176856 Change-Id: Ifa399a0b7b68e4add5a94ca148b32b2938a8666d
1 parent 7e2c425 commit 28e9d1f

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed

docs/advanced.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,21 @@ test case is linked in.
19401940
Note that *any* test case linked in makes the program valid for the purpose of
19411941
this check. In particular, even a disabled test case suffices.
19421942
1943+
### Enforcing Running At Least One Test Case
1944+
1945+
In addition to enforcing that tests are defined in the binary with
1946+
`--gtest_fail_if_no_test_linked`, it is also possible to enforce that a test
1947+
case was actually executed to ensure that resources are not consumed by tests
1948+
that do nothing.
1949+
1950+
To catch such optimization opportunities, run the test program with the
1951+
`--gtest_fail_if_no_test_selected` flag or set the
1952+
`GTEST_FAIL_IF_NO_TEST_SELECTED` environment variable to a value other than `0`.
1953+
1954+
A test is considered selected if it begins to run, even if it is later skipped
1955+
via `GTEST_SKIP`. Thus, `DISABLED` tests do not count as selected and neither do
1956+
tests that are not matched by `--gtest_filter`.
1957+
19431958
### Repeating the Tests
19441959
19451960
Once in a while you'll run into a test whose result is hit-or-miss. Perhaps it

googletest/src/gtest.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ GTEST_DEFINE_bool_(
269269
"True if and only if the test should fail if no test case (including "
270270
"disabled test cases) is linked.");
271271

272+
GTEST_DEFINE_bool_(
273+
fail_if_no_test_selected,
274+
testing::internal::BoolFromGTestEnv("fail_if_no_test_selected", false),
275+
"True if and only if the test should fail if no test case is selected to "
276+
"run. A test case is selected to run if it is not disabled and is matched "
277+
"by the filter flag so that it starts executing.");
278+
272279
GTEST_DEFINE_bool_(
273280
also_run_disabled_tests,
274281
testing::internal::BoolFromGTestEnv("also_run_disabled_tests", false),
@@ -6079,6 +6086,14 @@ bool UnitTestImpl::RunAllTests() {
60796086
TearDownEnvironment);
60806087
repeater->OnEnvironmentsTearDownEnd(*parent_);
60816088
}
6089+
} else if (GTEST_FLAG_GET(fail_if_no_test_selected)) {
6090+
// If there were no tests to run, bail if we were requested to be strict.
6091+
constexpr char kNoTestsSelectedMessage[] =
6092+
"No tests were selected to run. Please make sure at least one test "
6093+
"exists and is not disabled! If the test is sharded, you may have "
6094+
"defined more shards than test cases, which is wasteful.";
6095+
ColoredPrintf(GTestColor::kRed, "%s\n", kNoTestsSelectedMessage);
6096+
return false;
60826097
}
60836098

60846099
elapsed_time_ = timer.Elapsed();
@@ -6763,6 +6778,7 @@ static bool ParseGoogleTestFlag(const char* const arg) {
67636778
GTEST_INTERNAL_PARSE_FLAG(death_test_use_fork);
67646779
GTEST_INTERNAL_PARSE_FLAG(fail_fast);
67656780
GTEST_INTERNAL_PARSE_FLAG(fail_if_no_test_linked);
6781+
GTEST_INTERNAL_PARSE_FLAG(fail_if_no_test_selected);
67666782
GTEST_INTERNAL_PARSE_FLAG(filter);
67676783
GTEST_INTERNAL_PARSE_FLAG(internal_run_death_test);
67686784
GTEST_INTERNAL_PARSE_FLAG(list_tests);

googletest/test/googletest-fail-if-no-test-linked-test.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ def testSucceedsIfNoTestLinkedAndFlagNotSpecifiedWithWarningFile(self):
8080
)
8181
)
8282
warning_file_contents = open(warning_file, "r").read()
83-
self.assertEqual(
84-
warning_file_contents,
85-
"This test program does NOT link in any test case. Please make sure"
86-
" this is intended.\n",
87-
)
83+
self.assertIn("does NOT link", warning_file_contents)
8884

8985
def testFailsIfNoTestLinkedAndFlagSpecified(self):
9086
"""Tests the behavior of no test linked and flag specified."""
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3 # pylint: disable=g-interpreter-mismatch
2+
#
3+
# Copyright 2025, Google Inc.
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are
8+
# met:
9+
#
10+
# * Redistributions of source code must retain the above copyright
11+
# notice, this list of conditions and the following disclaimer.
12+
# * Redistributions in binary form must reproduce the above
13+
# copyright notice, this list of conditions and the following disclaimer
14+
# in the documentation and/or other materials provided with the
15+
# distribution.
16+
# * Neither the name of Google Inc. nor the names of its
17+
# contributors may be used to endorse or promote products derived from
18+
# this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
"""Tests for Google Test's --gtest_fail_if_no_test_selected flag."""
32+
33+
from googletest.test import gtest_test_utils
34+
35+
36+
# The command line flag for enabling the fail-if-no-test-selected behavior.
37+
FAIL_IF_NO_TEST_SELECTED_FLAG = "gtest_fail_if_no_test_selected"
38+
39+
# The environment variable for the test output warnings file.
40+
TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
41+
42+
43+
class GTestFailIfNoTestSelectedTest(gtest_test_utils.TestCase):
44+
"""Tests the --gtest_fail_if_no_test_selected flag."""
45+
46+
def Run(self, program_name, flags=None, env=None):
47+
"""Run the given program with the given flag.
48+
49+
Args:
50+
program_name: Name of the program to run.
51+
flags: The command line flags to pass to the program, or None.
52+
env: Dictionary with environment to pass to the subprocess.
53+
54+
Returns:
55+
True if the program exits with code 0, false otherwise.
56+
"""
57+
58+
exe_path = gtest_test_utils.GetTestExecutablePath(program_name)
59+
args = [exe_path]
60+
if flags is not None:
61+
args.extend(flags)
62+
process = gtest_test_utils.Subprocess(args, capture_stderr=False, env=env)
63+
return process.exited and process.exit_code == 0
64+
65+
def testSucceedsWhenFlagIsNotSetAndOnlyDisabledTestsPresent(self):
66+
"""Tests that no test selected results in success without the flag set."""
67+
self.assertTrue(
68+
self.Run("googletest-fail-if-no-test-linked-test-with-disabled-test_"),
69+
)
70+
71+
def testFailsWhenFlagIsSetAndOnlyDisabledTestsPresent(self):
72+
"""Tests that no test selected results in failure with the flag set."""
73+
self.assertFalse(
74+
self.Run(
75+
"googletest-fail-if-no-test-linked-test-with-disabled-test_",
76+
flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"],
77+
),
78+
)
79+
80+
def testSucceedsWhenFlagIsSetAndEnabledTestsPresent(self):
81+
"""Tests that a test running still succeeds when the flag is set."""
82+
self.assertTrue(
83+
self.Run(
84+
"googletest-fail-if-no-test-linked-test-with-enabled-test_",
85+
flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"],
86+
),
87+
)
88+
89+
90+
if __name__ == "__main__":
91+
gtest_test_utils.Main()

0 commit comments

Comments
 (0)