@@ -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+
272279GTEST_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);
0 commit comments