diff options
author | Derek Mauro <dmauro@google.com> | 2025-02-28 19:35:40 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2025-02-28 19:36:13 (GMT) |
commit | 24a9e940d481f992ba852599c78bb2217362847b (patch) | |
tree | 8eca9a8975bccdb227abab389483c2e64754bbe7 /googletest/src | |
parent | 72189081cae8b729422860b195bf2cad625b7eb4 (diff) | |
download | googletest-24a9e940d481f992ba852599c78bb2217362847b.zip googletest-24a9e940d481f992ba852599c78bb2217362847b.tar.gz googletest-24a9e940d481f992ba852599c78bb2217362847b.tar.bz2 |
Try to warn the user when test filters do not match any tests
PiperOrigin-RevId: 732204780
Change-Id: I2c4ccabd123e3b79c3dd8bc768a4cd9a576d282c
Diffstat (limited to 'googletest/src')
-rw-r--r-- | googletest/src/gtest-internal-inl.h | 4 | ||||
-rw-r--r-- | googletest/src/gtest.cc | 35 |
2 files changed, 39 insertions, 0 deletions
diff --git a/googletest/src/gtest-internal-inl.h b/googletest/src/gtest-internal-inl.h index cc6f004..6a39b93 100644 --- a/googletest/src/gtest-internal-inl.h +++ b/googletest/src/gtest-internal-inl.h @@ -826,6 +826,10 @@ class GTEST_API_ UnitTestImpl { bool catch_exceptions() const { return catch_exceptions_; } private: + // Returns true if a warning should be issued if no tests match the test + // filter flag. + bool ShouldWarnIfNoTestsMatchFilter() const; + struct CompareTestSuitesByPointer { bool operator()(const TestSuite* lhs, const TestSuite* rhs) const { return lhs->name_ < rhs->name_; diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 26959df..09af151 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -6113,6 +6113,17 @@ bool UnitTestImpl::RunAllTests() { environments_.clear(); } + // Try to warn the user if no tests matched the test filter. + if (ShouldWarnIfNoTestsMatchFilter()) { + const std::string filter_warning = + std::string("filter \"") + GTEST_FLAG_GET(filter) + + "\" did not match any test; no tests were run\n"; + ColoredPrintf(GTestColor::kRed, "WARNING: %s", filter_warning.c_str()); +#if GTEST_HAS_FILE_SYSTEM + AppendToTestWarningsOutputFile(filter_warning); +#endif // GTEST_HAS_FILE_SYSTEM + } + if (!gtest_is_initialized_before_run_all_tests) { ColoredPrintf( GTestColor::kRed, @@ -6281,6 +6292,30 @@ int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { return num_selected_tests; } +// Returns true if a warning should be issued if no tests match the test filter +// flag. We can't simply count the number of tests that ran because, for +// instance, test sharding and death tests might mean no tests are expected to +// run in this process, but will run in another process. +bool UnitTestImpl::ShouldWarnIfNoTestsMatchFilter() const { + if (total_test_count() == 0) { + // No tests were linked in to program. + // This case is handled by a different warning. + return false; + } + const PositiveAndNegativeUnitTestFilter gtest_flag_filter( + GTEST_FLAG_GET(filter)); + for (auto* test_suite : test_suites_) { + const std::string& test_suite_name = test_suite->name_; + for (TestInfo* test_info : test_suite->test_info_list()) { + const std::string& test_name = test_info->name_; + if (gtest_flag_filter.MatchesTest(test_suite_name, test_name)) { + return false; + } + } + } + return true; +} + // Prints the given C-string on a single line by replacing all '\n' // characters with string "\\n". If the output takes more than // max_length characters, only prints the first max_length characters |