Can I give better names to value-parameterized tests in gtest?
Asked Answered
D

3

31

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
Damnedest answered 31/7, 2013 at 7:24 Comment(1)
At least if there's an error, GTest gives this: Test/FooTest.DoesBlah/0, where GetParam() = (000000013F6F2C00 pointing to "meeny")Damnedest
S
16

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.

Swank answered 23/8, 2016 at 5:8 Comment(2)
From the link: the easiest solution is to use ::testing::PrintToStringParamName as 4th parameter.Listlessness
Link should be github.com/google/googletest/blob/…Raylenerayless
M
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.
Mastic answered 22/1, 2020 at 10:20 Comment(0)
L
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.

Leveridge answered 24/2, 2015 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.