Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Test Fixtures

Tags:

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

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?

like image 601
bei Avatar asked Aug 23 '10 16:08

bei


People also ask

What is test fixture in Google Test?

If you find yourself writing two or more tests that operate on similar data, you can use a test fixture. This allows you to reuse the same configuration of objects for several different tests. To create a fixture: Derive a class from ::testing::Test .

What is Google Test suite?

The Google Home Test Suite is a web application that allows you to self-test your smart home Action. The Test Suite automatically generates and runs test cases based on the devices and traits associated with your account.


2 Answers

myPhrase1 and myPhrase2 are local to the setup method, not the test fixture.

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);  } 
like image 140
Billy ONeal Avatar answered Sep 19 '22 12:09

Billy ONeal


myPhrase1 and myPhrase2 are declared as local variables in the SetUp function. You need to declare them as members of the class:

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); } 
like image 32
Bill Avatar answered Sep 22 '22 12:09

Bill