C++ 谷歌测试装置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3549540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:05:33  来源:igfitidea点击:

Google Test Fixtures

c++testinggoogletest

提问by jiake

I'm trying to understand how the Google Test Fixtures work.

我试图了解 Google 测试装置的工作原理。

Say I have the following code:

假设我有以下代码:

class PhraseTest : public ::testing::Test
{
     protected:
     virtual void SetUp()
     {      
         phraseClass * myPhrase1 = new createPhrase("1234567890");
         phraseClass * myPhrase2 = new createPhrase("1234567890");  
     }

     virtual void TearDown()
    {
        delete *myPhrase1;
        delete *myPhrase2;  
     }
};



TEST_F(PhraseTest, OperatorTest)
{
    ASSERT_TRUE(*myPhrase1 == *myPhrase2);

}

When I compile, why does it say "myPhrase1" and "myPhrase2" are undeclared in the TEST_F?

当我编译时,为什么它说 TEST_F 中未声明“myPhrase1”和“myPhrase2”?

回答by Billy ONeal

myPhrase1and myPhrase2are local to the setup method, not the test fixture.

myPhrase1并且myPhrase2是设置方法的本地,而不是测试夹具。

What you wanted was:

你想要的是:

class PhraseTest : public ::testing::Test
{
protected:
     phraseClass * myPhrase1;
     phraseClass * myPhrase2;
     virtual void SetUp()
     {      
         myPhrase1 = new createPhrase("1234567890");
         myPhrase2 = new createPhrase("1234567890");  
     }

     virtual void TearDown()
     {
        delete myPhrase1;
        delete myPhrase2;  
     }
};

TEST_F(PhraseTest, OperatorTest)
{
    ASSERT_TRUE(*myPhrase1 == *myPhrase2);

}

回答by Bill

myPhrase1and myPhrase2are declared as local variables in the SetUpfunction. You need to declare them as members of the class:

myPhrase1并且myPhrase2SetUp函数中被声明为局部变量。您需要将它们声明为类的成员:

class PhraseTest : public ::testing::Test
{
  protected:

  virtual void SetUp()
  {      
    myPhrase1 = new createPhrase("1234567890");
    myPhrase2 = new createPhrase("1234567890");  
  }

  virtual void TearDown()
  {
    delete myPhrase1;
    delete myPhrase2;  
  }

  phraseClass* myPhrase1;
  phraseClass* myPhrase2;
};

TEST_F(PhraseTest, OperatorTest)
{
  ASSERT_TRUE(*myPhrase1 == *myPhrase2);
}