Can I give better names to value-parameterized tes

2020-05-25 10:54发布

问题:

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

回答1:

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



回答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.



回答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.


标签: googletest