当我创建常规 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
然后将运行所有已注册的 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());
关于c++ - 访问 Google Test 参数化测试用例 (`TEST_P` 中的测试信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833494/