Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GTest-如何通过设置方法为多种用途准备数据?_C++_Unit Testing_Googletest - Fatal编程技术网

C++ GTest-如何通过设置方法为多种用途准备数据?

C++ GTest-如何通过设置方法为多种用途准备数据?,c++,unit-testing,googletest,C++,Unit Testing,Googletest,我正在尝试运行一些google测试,每个测试装置中都有大量代码要重复,因此我希望代码尽可能简短,并且我希望使用Testing::test parent类的子类的SetUp方法,但是测试装置无法识别SetUp中的变量 这是我能想到的最简单的例子: class FooTest: public testing::Test { protected: virtual void SetUp() // using void SetUp() override does n

我正在尝试运行一些google测试,每个测试装置中都有大量代码要重复,因此我希望代码尽可能简短,并且我希望使用Testing::test parent类的子类的SetUp方法,但是测试装置无法识别SetUp中的变量

这是我能想到的最简单的例子:

class FooTest: public testing::Test
      {
      protected:
        virtual void SetUp() // using void SetUp() override does not help
        {
          int FooVar = 911;
        }

        virtual void TearDown()
        {
        }
      };

TEST_F(FooTest, SampleTest)
{
  // FooTest::SetUp(); // This does not help as well
  EXPECT_EQ(911, FooVar);
}
当我试图编译这段代码时,它显示了一个错误,即FooVar没有在此范围内声明。我怎样才能修好它?
非常感谢您的帮助。

FooVar
设置
方法中的局部变量。如果要在测试夹具中使用它,它需要是类成员:

class FooTest: public testing::Test
{
      protected:

      int FooVar;
      virtual void SetUp() override
      {
        this.FooVar = 911;
      }
};

在本例中,如果您仅设置整数类型,则应将其设置为常量成员变量。

您需要将
FooVar
声明为类的成员。目前,它是
SetUp
方法中的一个局部变量。您正在
int FooVar=911中声明
FooVar
。编译器怎么可能显示那个错误?@M.A谢谢,我没意识到。你能补充一个答案让我接受吗?