gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:02 27 4
gpt4 key购买 nike

Google Test C++ 单元测试框架提供了执行 parameterised tests 的能力.要访问给定测试的参数,文档告诉我派生一个子类并调用 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>.
};

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

我究竟可以在哪里(或何时)调用 GetParam()?我知道我可以在 TEST_P(...) { ... } 宏的主体中调用它,但是怎么样:

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

最佳答案

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

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

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

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

关于c++ - 在 Google Test 中,我可以从构造函数中调用 GetParam() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com