Skip to content

Commit 24a9e94

Browse files
derekmaurocopybara-github
authored andcommitted
Try to warn the user when test filters do not match any tests
PiperOrigin-RevId: 732204780 Change-Id: I2c4ccabd123e3b79c3dd8bc768a4cd9a576d282c
1 parent 7218908 commit 24a9e94

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

googletest/src/gtest-internal-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,10 @@ class GTEST_API_ UnitTestImpl {
826826
bool catch_exceptions() const { return catch_exceptions_; }
827827

828828
private:
829+
// Returns true if a warning should be issued if no tests match the test
830+
// filter flag.
831+
bool ShouldWarnIfNoTestsMatchFilter() const;
832+
829833
struct CompareTestSuitesByPointer {
830834
bool operator()(const TestSuite* lhs, const TestSuite* rhs) const {
831835
return lhs->name_ < rhs->name_;

googletest/src/gtest.cc

+35
Original file line numberDiff line numberDiff line change
@@ -6113,6 +6113,17 @@ bool UnitTestImpl::RunAllTests() {
61136113
environments_.clear();
61146114
}
61156115

6116+
// Try to warn the user if no tests matched the test filter.
6117+
if (ShouldWarnIfNoTestsMatchFilter()) {
6118+
const std::string filter_warning =
6119+
std::string("filter \"") + GTEST_FLAG_GET(filter) +
6120+
"\" did not match any test; no tests were run\n";
6121+
ColoredPrintf(GTestColor::kRed, "WARNING: %s", filter_warning.c_str());
6122+
#if GTEST_HAS_FILE_SYSTEM
6123+
AppendToTestWarningsOutputFile(filter_warning);
6124+
#endif // GTEST_HAS_FILE_SYSTEM
6125+
}
6126+
61166127
if (!gtest_is_initialized_before_run_all_tests) {
61176128
ColoredPrintf(
61186129
GTestColor::kRed,
@@ -6281,6 +6292,30 @@ int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
62816292
return num_selected_tests;
62826293
}
62836294

6295+
// Returns true if a warning should be issued if no tests match the test filter
6296+
// flag. We can't simply count the number of tests that ran because, for
6297+
// instance, test sharding and death tests might mean no tests are expected to
6298+
// run in this process, but will run in another process.
6299+
bool UnitTestImpl::ShouldWarnIfNoTestsMatchFilter() const {
6300+
if (total_test_count() == 0) {
6301+
// No tests were linked in to program.
6302+
// This case is handled by a different warning.
6303+
return false;
6304+
}
6305+
const PositiveAndNegativeUnitTestFilter gtest_flag_filter(
6306+
GTEST_FLAG_GET(filter));
6307+
for (auto* test_suite : test_suites_) {
6308+
const std::string& test_suite_name = test_suite->name_;
6309+
for (TestInfo* test_info : test_suite->test_info_list()) {
6310+
const std::string& test_name = test_info->name_;
6311+
if (gtest_flag_filter.MatchesTest(test_suite_name, test_name)) {
6312+
return false;
6313+
}
6314+
}
6315+
}
6316+
return true;
6317+
}
6318+
62846319
// Prints the given C-string on a single line by replacing all '\n'
62856320
// characters with string "\\n". If the output takes more than
62866321
// max_length characters, only prints the first max_length characters

googletest/test/googletest-filter-unittest.py

+19
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
9898
SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
9999

100+
# The environment variable for the test warnings output file.
101+
TEST_WARNINGS_OUTPUT_FILE = 'TEST_WARNINGS_OUTPUT_FILE'
102+
100103
# The command line flag for specifying the test filters.
101104
FILTER_FLAG = 'gtest_filter'
102105

@@ -419,6 +422,22 @@ def testBadFilter(self):
419422
self.RunAndVerify('BadFilter', [])
420423
self.RunAndVerifyAllowingDisabled('BadFilter', [])
421424

425+
def testBadFilterWithWarningFile(self):
426+
"""Tests the warning file when a filter that matches nothing."""
427+
428+
warning_file = os.path.join(
429+
gtest_test_utils.GetTempDir(), 'testBadFilterWithWarningFile'
430+
)
431+
extra_env = {TEST_WARNINGS_OUTPUT_FILE: warning_file}
432+
args = ['--%s=%s' % (FILTER_FLAG, 'BadFilter')]
433+
InvokeWithModifiedEnv(extra_env, RunAndReturnOutput, args)
434+
with open(warning_file, 'r') as f:
435+
warning_file_contents = f.read()
436+
self.assertEqual(
437+
warning_file_contents,
438+
'filter "BadFilter" did not match any test; no tests were run\n',
439+
)
440+
422441
def testFullName(self):
423442
"""Tests filtering by full name."""
424443

0 commit comments

Comments
 (0)