在 Google 测试参数化测试用例 (`TEST_P`) 中访问测试信息
Accessing test info in Google Test parameterized test case (`TEST_P`)
当我创建常规 TEST
(或 TEST_F
)时,我可以访问存储在 test_info_
中的测试用例信息,例如:
TEST_F(MyTestSuite, TestCaseOne)
{
// ...
test_info_->name(); // will return "TestCaseOne"
}
我想在使用参数化 (TEST_P
) 变体时访问此类信息,它允许我定义基于夹具的测试。
深入了解,我可以看到 TEST_P
的工作方式与她的堂兄弟 TEST
和 TEST_F
完全不同,因为它 注册 通过调用 ::testing::UnitTest::GetInstance()->parameterized_test_registry().GetTestCasePatternHolder<test_case_name>(#test_case_name, __FILE__, __LINE__)->AddTestPattern(...)
方法创建新的测试用例。我知道从 TestWithParam
继承的 class 将 运行 所有已注册的 TEST_P
测试用例。
有没有办法访问(运行时间或编译时间)TEST_P
的名称(字符串)?
TestInfo
实例实际上有一个 getter。来自 the documentation:
To obtain a TestInfo
object for the currently running test, call
current_test_info()
on the UnitTest
singleton object:
// Gets information about the currently running test.
// Do NOT delete the returned object - it's managed by the UnitTest class.
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
printf("We are in test %s of test case %s.\n",
test_info->name(), test_info->test_case_name());
当我创建常规 TEST
(或 TEST_F
)时,我可以访问存储在 test_info_
中的测试用例信息,例如:
TEST_F(MyTestSuite, TestCaseOne)
{
// ...
test_info_->name(); // will return "TestCaseOne"
}
我想在使用参数化 (TEST_P
) 变体时访问此类信息,它允许我定义基于夹具的测试。
深入了解,我可以看到 TEST_P
的工作方式与她的堂兄弟 TEST
和 TEST_F
完全不同,因为它 注册 通过调用 ::testing::UnitTest::GetInstance()->parameterized_test_registry().GetTestCasePatternHolder<test_case_name>(#test_case_name, __FILE__, __LINE__)->AddTestPattern(...)
方法创建新的测试用例。我知道从 TestWithParam
继承的 class 将 运行 所有已注册的 TEST_P
测试用例。
有没有办法访问(运行时间或编译时间)TEST_P
的名称(字符串)?
TestInfo
实例实际上有一个 getter。来自 the documentation:
To obtain a
TestInfo
object for the currently running test, callcurrent_test_info()
on theUnitTest
singleton object:// Gets information about the currently running test. // Do NOT delete the returned object - it's managed by the UnitTest class. const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); printf("We are in test %s of test case %s.\n", test_info->name(), test_info->test_case_name());