c++ - 访问 Google Test 参数化测试用例 (`TEST_P` 中的测试信息)

标签 c++ unit-testing googletest googlemock

当我创建常规 TEST 时(或 TEST_F ),我可以访问存储在 test_info_ 中的测试用例信息,例如:

TEST_F(MyTestSuite, TestCaseOne) 
{
  // ... 
  test_info_->name(); // will return "TestCaseOne"
}

我想在使用参数化 ( TEST_P) 变体时访问此类信息,它允许我定义基于夹具的测试。

深入观察,我可以看到 TEST_P工作方式与她的堂兄弟完全不同 TESTTEST_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, 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());

关于c++ - 访问 Google Test 参数化测试用例 (`TEST_P` 中的测试信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833494/

相关文章:

c++ - 在构造函数中带有参数的 blitz++ 类的 GoogleTest Fixture

c++ - 原子变量的多重赋值是原子操作吗?

c# - 如何将对象指针从 DLL 传递到 DLL?

c++ - 对 vector 字符串进行单元测试

c++ - 在测试中,有没有办法防御来自未定义行为的 "correct"结果?

c# - 单元测试,模拟 - 简单案例 : Service - Repository

c++ - 我如何解析 H264 文件和帧

c++ - 使用 stringstream 对字符串进行标记,其中最后一个字符是分隔符

c++ - 如何对回调函数抛出的异常进行单元测试

c# - 在单元测试中模拟 HttpPostedFile