gpt4 book ai didi

c++ - 如何访问谷歌测试 fixture 静态变量

转载 作者:行者123 更新时间:2023-11-28 21:36:20 38 4
gpt4 key购买 nike

我想用谷歌测试来测试我的一门类(class)。此类的初始化需要大量时间和资源,所以我只想对所有测试用例执行一次,因此我尝试将 fixture 与 SetUpTestSuite() 一起使用。在我的 fixture 中,我声明了一个变量:

static MyClassToBeTested my_class;

在我的测试用例中,我想访问 my_class 变量。

在编译过程中出现以下错误:

undefined reference to 'MyTest::my_class'

我试图通过简单的 my_classMyTest::my_class 访问它:

class MyTest : public ::testing::Test {
protected:
static MyClassToBeTested my_class;
static void SetUpTestSuite() {
//doing some stuff here
}
};
TEST_F(MyTest, first_test) {
ASSERT_EQ(my_class.foo(), 5);
}

最佳答案

你必须定义你的静态变量

class MyTest : public ::testing::Test {
protected:
static MyClassToBeTested my_class;
static void SetUpTestSuite() {
//doing some stuff here
}
};

MyClassToBeTested MyTest::my_class;

关于c++ - 如何访问谷歌测试 fixture 静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453359/

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