views:

74

answers:

2

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?

+1  A: 

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

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

What you wanted was:

EDIT: Actually the members might need to be public or protected (rather than private, as they are here)... I cannot remember exactly.

class PhraseTest : public ::testing::Test
{
     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);

}
Billy ONeal
Thanks! That fixed it!
Bei337
protected, says http://code.google.com/p/googletest/source/browse/trunk/samples/sample3_unittest.cc
Bill
Ya, I had to use protected.
Bei337