在 Google Test 中,我可以从构造函数中调用 GetParam() 吗?

det*_*tly 5 c++ googletest

Google Test C++ 单元测试框架提供了进行参数化测试的能力。要访问给定测试的参数,文档告诉我派生一个子类并调用GetParam()

class FooTest : public ::testing::TestWithParam<const char*> {
  // You can implement all the usual fixture class members here.
  // To access the test parameter, call GetParam() from class
  // TestWithParam<T>.
};
Run Code Online (Sandbox Code Playgroud)

我在文档或源代码中找不到比这更具体的内容(据我所知)。

我到底可以在哪里(或什么时候)打电话GetParam()?我知道我可以在宏体内调用它TEST_P(...) { ... },但是怎么样:

  • SetUp()方法中FooTest()?
  • FooTest()在?的构造函数中
  • 在初始化列表中FooTest()

Pio*_*ycz 5

是的你可以。您可以假设是基类GetParam()的方法。::testing::TestWithParam

class FooTest : public ::testing::TestWithParam<const char*> {
  std::string name;
  FooTest() : name(GetParam()) {} 
};
Run Code Online (Sandbox Code Playgroud)

使用C++11 - 您甚至可以直接在类中初始化成员:

class FooTest : public ::testing::TestWithParam<const char*> {
  std::string name = GetParam();
};
Run Code Online (Sandbox Code Playgroud)