我可以在gtest中为值参数化测试提供更好的名称吗?

时间:2013-07-31 07:24:29

标签: googletest

我在gtest中使用值参数化测试。例如,如果我写

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

然后在输出中我看到测试名称,如

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

有没有办法让这些名字更有意义?我想看看

InstantiationName/FooTest.DoesBlah/meeny
InstantiationName/FooTest.DoesBlah/miny
InstantiationName/FooTest.DoesBlah/moe

3 个答案:

答案 0 :(得分:9)

INSTANTIATE_TEST_CASE_P接受可选的第4个参数,可用于此目的。请参阅https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest-param-test.h#L1409

答案 1 :(得分:3)

现在在INSTANTIATE_TEST_SUITE_P中可用。

INSTANTIATE_TEST_SUITE_P()的最后一个可选参数允许 用户指定生成自定义测试名称的函数或函子 后缀取决于测试参数。

感兴趣的人也来自this section

// 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.

答案 2 :(得分:2)

两种方式:(http://osdir.com/ml/googletestframework/2011-09/msg00005.html

1)修补现有的PrettyUnitTestPrinter以打印测试名称;类似的东西:

--- 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)编写一个新的TestListener来打印您喜欢的测试结果。 (https://code.google.com/p/googletest/source/browse/trunk/samples/sample9_unittest.cc)GTest允许注册一个新的测试监听器(并取消注册内置的默认值),允许非常灵活的测试输出自定义。请参阅链接以获取示例代码。