22

我试图了解 Google 测试装置是如何工作的。

假设我有以下代码:

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);

}

当我编译时,为什么它说myPhrase1并且myPhrase2未在TEST_F

4

2 回答 2

40

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

你想要的是:

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);

}
于 2010-08-23T16:38:17.647 回答
3

myPhrase1myPhrase2SetUp函数中声明为局部变量。您需要将它们声明为该类的成员:

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);
}
于 2010-08-23T16:37:03.157 回答