如何使用Google Test Fixture静态变量

问题描述 投票:0回答:1

我想用google test测试我的课程之一。此类的初始化需要很多时间和资源,因此对于所有测试用例,我只想执行一次,因此,我尝试将Fixture与SetUpTestSuite()一起使用。在我的装置中,我声明了一个变量:

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);
}
c++ testing static googletest fixtures
1个回答
0
投票

您必须定义您的静态变量

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

MyClassToBeTested MyTest::my_class;
© www.soinside.com 2019 - 2024. All rights reserved.