【问题标题】:Google Test Fixtures谷歌测试装置
【发布时间】:2011-04-02 17:57:42
【问题描述】:

我正在尝试了解 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);

}

我编译的时候为什么说myPhrase1myPhrase2TEST_F中没有声明?

【问题讨论】:

  • 另一个问题:
    为什么要使用“delete *myPhrase1;”?
    我认为使用删除的适当方法是“delete myPhrase1;”。

标签: c++ unit-testing testing googletest test-fixture


【解决方案1】:

myPhrase1myPhrase2 是本地设置方法,而不是测试夹具。

你想要的是:

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

}

【讨论】:

  • @BillyONeal 是否暗示 SetUp() 中的代码有效?也就是说,您应该对要在 SetUp() 中使用的所有内容(这里是构造函数)使用普通的 TEST() (而不是 TEST_F() )?
  • @DavidDoria 我认为 SetUp 与 xunit 相似。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 2012-08-17
    • 2014-04-04
    • 1970-01-01
    • 2019-12-19
    • 2011-06-14
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多