New command line parameter: list_tests_with_location Allows to see the file and line where the test is defined

This commit is contained in:
David Schuldenfrei 2018-07-11 17:03:06 +03:00
parent 7eae4ea4e1
commit 8989d41bc1
3 changed files with 34 additions and 6 deletions

View File

@ -116,6 +116,11 @@ GTEST_DECLARE_string_(filter);
// are actually run if the flag is provided. // are actually run if the flag is provided.
GTEST_DECLARE_bool_(list_tests); GTEST_DECLARE_bool_(list_tests);
// This flag causes the Google Test to list tests with their location
// (file and line number). None of the tests listed
// are actually run if the flag is provided.
GTEST_DECLARE_bool_(list_tests_with_location);
// This flag controls whether Google Test emits a detailed XML report to a file // This flag controls whether Google Test emits a detailed XML report to a file
// in addition to its normal textual output. // in addition to its normal textual output.
GTEST_DECLARE_string_(output); GTEST_DECLARE_string_(output);

View File

@ -84,6 +84,7 @@ const char kCatchExceptionsFlag[] = "catch_exceptions";
const char kColorFlag[] = "color"; const char kColorFlag[] = "color";
const char kFilterFlag[] = "filter"; const char kFilterFlag[] = "filter";
const char kListTestsFlag[] = "list_tests"; const char kListTestsFlag[] = "list_tests";
const char kListTestsWithLocationFlag[] = "list_tests_with_location";
const char kOutputFlag[] = "output"; const char kOutputFlag[] = "output";
const char kPrintTimeFlag[] = "print_time"; const char kPrintTimeFlag[] = "print_time";
const char kPrintUTF8Flag[] = "print_utf8"; const char kPrintUTF8Flag[] = "print_utf8";
@ -165,6 +166,7 @@ class GTestFlagSaver {
filter_ = GTEST_FLAG(filter); filter_ = GTEST_FLAG(filter);
internal_run_death_test_ = GTEST_FLAG(internal_run_death_test); internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
list_tests_ = GTEST_FLAG(list_tests); list_tests_ = GTEST_FLAG(list_tests);
list_tests_with_location_ = GTEST_FLAG(list_tests_with_location);
output_ = GTEST_FLAG(output); output_ = GTEST_FLAG(output);
print_time_ = GTEST_FLAG(print_time); print_time_ = GTEST_FLAG(print_time);
print_utf8_ = GTEST_FLAG(print_utf8); print_utf8_ = GTEST_FLAG(print_utf8);
@ -187,6 +189,7 @@ class GTestFlagSaver {
GTEST_FLAG(filter) = filter_; GTEST_FLAG(filter) = filter_;
GTEST_FLAG(internal_run_death_test) = internal_run_death_test_; GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
GTEST_FLAG(list_tests) = list_tests_; GTEST_FLAG(list_tests) = list_tests_;
GTEST_FLAG(list_tests_with_location) = list_tests_with_location_;
GTEST_FLAG(output) = output_; GTEST_FLAG(output) = output_;
GTEST_FLAG(print_time) = print_time_; GTEST_FLAG(print_time) = print_time_;
GTEST_FLAG(print_utf8) = print_utf8_; GTEST_FLAG(print_utf8) = print_utf8_;
@ -209,6 +212,7 @@ class GTestFlagSaver {
std::string filter_; std::string filter_;
std::string internal_run_death_test_; std::string internal_run_death_test_;
bool list_tests_; bool list_tests_;
bool list_tests_with_location_;
std::string output_; std::string output_;
bool print_time_; bool print_time_;
bool print_utf8_; bool print_utf8_;
@ -722,7 +726,7 @@ class GTEST_API_ UnitTestImpl {
int FilterTests(ReactionToSharding shard_tests); int FilterTests(ReactionToSharding shard_tests);
// Prints the names of the tests matching the user-specified filter flag. // Prints the names of the tests matching the user-specified filter flag.
void ListTestsMatchingFilter(); void ListTestsMatchingFilter(bool list_location);
const TestCase* current_test_case() const { return current_test_case_; } const TestCase* current_test_case() const { return current_test_case_; }
TestInfo* current_test_info() { return current_test_info_; } TestInfo* current_test_info() { return current_test_info_; }

View File

@ -231,6 +231,9 @@ GTEST_DEFINE_string_(
GTEST_DEFINE_bool_(list_tests, false, GTEST_DEFINE_bool_(list_tests, false,
"List all tests without running them."); "List all tests without running them.");
GTEST_DEFINE_bool_(list_tests_with_location, false,
"List all tests, and their location, without running them.");
// The net priority order after flag processing is thus: // The net priority order after flag processing is thus:
// --gtest_output command line flag // --gtest_output command line flag
// GTEST_OUTPUT environment variable // GTEST_OUTPUT environment variable
@ -4994,9 +4997,15 @@ bool UnitTestImpl::RunAllTests() {
// Lists the tests and exits if the --gtest_list_tests flag was specified. // Lists the tests and exits if the --gtest_list_tests flag was specified.
if (GTEST_FLAG(list_tests)) { if (GTEST_FLAG(list_tests)) {
// This must be called *after* FilterTests() has been called. // This must be called *after* FilterTests() has been called.
ListTestsMatchingFilter(); ListTestsMatchingFilter(false);
return true; return true;
} }
if (GTEST_FLAG(list_tests_with_location)) {
// This must be called *after* FilterTests() has been called.
ListTestsMatchingFilter(true);
return true;
}
random_seed_ = GTEST_FLAG(shuffle) ? random_seed_ = GTEST_FLAG(shuffle) ?
GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0; GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;
@ -5276,7 +5285,7 @@ static void PrintOnOneLine(const char* str, int max_length) {
} }
// Prints the names of the tests matching the user-specified filter flag. // Prints the names of the tests matching the user-specified filter flag.
void UnitTestImpl::ListTestsMatchingFilter() { void UnitTestImpl::ListTestsMatchingFilter(bool list_location) {
// Print at most this many characters for each type/value parameter. // Print at most this many characters for each type/value parameter.
const int kMaxParamLength = 250; const int kMaxParamLength = 250;
@ -5299,7 +5308,11 @@ void UnitTestImpl::ListTestsMatchingFilter() {
} }
printf("\n"); printf("\n");
} }
printf(" %s", test_info->name()); printf(" ");
if (list_location) {
printf("%d;%s;", test_info->line(), test_info->file());
}
printf("%s", test_info->name());
if (test_info->value_param() != NULL) { if (test_info->value_param() != NULL) {
printf(" # %s = ", kValueParamLabel); printf(" # %s = ", kValueParamLabel);
// We print the value parameter on a single line to make the // We print the value parameter on a single line to make the
@ -5582,6 +5595,9 @@ static const char kColorEncodedHelpMessage[] =
" @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n" " @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n"
" List the names of all tests instead of running them. The name of\n" " List the names of all tests instead of running them. The name of\n"
" TEST(Foo, Bar) is \"Foo.Bar\".\n" " TEST(Foo, Bar) is \"Foo.Bar\".\n"
" @G--" GTEST_FLAG_PREFIX_ "list_tests_with_location@D\n"
" List the names of all tests, and their location instead of running them. The name of\n"
" TEST(Foo, Bar) is \"Foo.Bar\", and the lcation is lineNumber;fileName.\n"
" @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS" " @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS"
"[@G-@YNEGATIVE_PATTERNS]@D\n" "[@G-@YNEGATIVE_PATTERNS]@D\n"
" Run only the tests whose name matches one of the positive patterns but\n" " Run only the tests whose name matches one of the positive patterns but\n"
@ -5627,8 +5643,9 @@ static const char kColorEncodedHelpMessage[] =
" Do not report exceptions as test failures. Instead, allow them\n" " Do not report exceptions as test failures. Instead, allow them\n"
" to crash the program or throw a pop-up (on Windows).\n" " to crash the program or throw a pop-up (on Windows).\n"
"\n" "\n"
"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set " "Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, and "
"the corresponding\n" "@G--" GTEST_FLAG_PREFIX_ "list_tests_with_location@D "
"you can alternatively set the corresponding\n"
"environment variable of a flag (all letters in upper-case). For example, to\n" "environment variable of a flag (all letters in upper-case). For example, to\n"
"disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_ "disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_
"color=no@D or set\n" "color=no@D or set\n"
@ -5655,6 +5672,8 @@ static bool ParseGoogleTestFlag(const char* const arg) {
ParseStringFlag(arg, kInternalRunDeathTestFlag, ParseStringFlag(arg, kInternalRunDeathTestFlag,
&GTEST_FLAG(internal_run_death_test)) || &GTEST_FLAG(internal_run_death_test)) ||
ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) || ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) ||
ParseBoolFlag(arg, kListTestsWithLocationFlag,
&GTEST_FLAG(list_tests_with_location)) ||
ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) || ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) ||
ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) || ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) ||
ParseBoolFlag(arg, kPrintUTF8Flag, &GTEST_FLAG(print_utf8)) || ParseBoolFlag(arg, kPrintUTF8Flag, &GTEST_FLAG(print_utf8)) ||