【问题标题】:Accessing test info in Google Test parameterized test case (`TEST_P`)在 Google Test 参数化测试用例 (`TEST_P`) 中访问测试信息
【发布时间】:2015-08-05 13:08:43
【问题描述】:

当我创建一个常规的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_Ps 测试用例。

有没有办法访问(运行时或编译时)TEST_P 的名称(字符串)?

【问题讨论】:

    标签: c++ unit-testing googletest googlemock


    【解决方案1】:

    TestInfo 实例实际上有一个 getter。来自the documentation

    要获取当前运行测试的TestInfo 对象,请调用 current_test_info()UnitTest 单例对象上:

    // 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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 2022-06-27
      相关资源
      最近更新 更多