29

I use value-parameterized tests in gtest. For example, if I write

INSTANTIATE_TEST_CASE_P(InstantiationName,
                    FooTest,
                    ::testing::Values("meeny", "miny", "moe"));

then in the output I see test names such as

InstantiationName/FooTest.DoesBlah/0 for "meeny"
InstantiationName/FooTest.DoesBlah/1 for "miny"
InstantiationName/FooTest.DoesBlah/2 for "moe" 

Is there any way to make these names more meaningful? I'd like to see

InstantiationName/FooTest.DoesBlah/meeny
InstantiationName/FooTest.DoesBlah/miny
InstantiationName/FooTest.DoesBlah/moe
Lev
  • 6,487
  • 6
  • 28
  • 29
  • At least if there's an error, GTest gives this: `Test/FooTest.DoesBlah/0, where GetParam() = (000000013F6F2C00 pointing to "meeny")` – Lev Jul 31 '13 at 14:00

3 Answers3

15

INSTANTIATE_TEST_CASE_P accepts an optional 4th argument which can be used for this purpose. See https://github.com/google/googletest/blob/fbef0711cfce7b8f149aac773d30ae48ce3e166c/googletest/include/gtest/gtest-param-test.h#L444.

ksb
  • 670
  • 8
  • 19
  • 1
    From the link: the easiest solution is to use `::testing::PrintToStringParamName` as 4th parameter. – Bentoy13 Jul 10 '18 at 15:34
  • Link should be https://github.com/google/googletest/blob/fbef0711cfce7b8f149aac773d30ae48ce3e166c/googletest/include/gtest/gtest-param-test.h#L444 – Mark Nov 10 '20 at 09:32
3

This is now available in INSTANTIATE_TEST_SUITE_P.

The optional last argument to INSTANTIATE_TEST_SUITE_P() allows the user to specify a function or functor that generates custom test name suffixes based on the test parameters.

Of interest is also this section in the source:

// A user can teach this function how to print a class type T by
// defining either operator<<() or PrintTo() in the namespace that
// defines T.  More specifically, the FIRST defined function in the
// following list will be used (assuming T is defined in namespace
// foo):
//
//   1. foo::PrintTo(const T&, ostream*)
//   2. operator<<(ostream&, const T&) defined in either foo or the
//      global namespace.
Zitrax
  • 19,036
  • 20
  • 88
  • 110
2

Two ways: (http://osdir.com/ml/googletestframework/2011-09/msg00005.html)

1) Patch the existing PrettyUnitTestPrinter to print test names; something like:

--- a/gtest-1.7.0/src/gtest.cc
+++ b/gtest-1.7.0/src/gtest.cc
@@ -2774,6 +2774,7 @@ void PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) {
 void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
   ColoredPrintf(COLOR_GREEN,  "[ RUN      ] ");
   PrintTestName(test_info.test_case_name(), test_info.name());
+  PrintFullTestCommentIfPresent(test_info);
   printf("\n");
   fflush(stdout);
 }

2) Write a new TestListener to print test results however you like. (https://code.google.com/p/googletest/source/browse/trunk/samples/sample9_unittest.cc) GTest allows registering a new test listener (and un-registering the builtin default), allowing pretty flexible customization of test output. See the link for example code.

Thelema
  • 14,257
  • 6
  • 27
  • 35