2010-08-23 69 views
6

我在试着了解Google测试夹具是如何工作的。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未申报?

+2

另一个问题:
为什么您正在使用 “删除* myPhrase1;”?
我认为使用delete的适当方法是“delete myPhrase1;”。 – Zihan 2013-10-04 15:03:22

回答

15

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

} 
+0

谢谢!这解决了它! – jiake 2010-08-23 16:39:35

+0

保护,说http://code.google.com/p/googletest/source/browse/trunk/samples/sample3_unittest.cc – Bill 2010-08-23 16:40:45

+0

雅,我不得不使用保护。 – jiake 2010-08-23 16:44:06

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

在两个删除语句中,您应该只传递指针,而不要考虑它们。 – qed 2014-03-06 16:18:30