Home > Back-end >  Variables in Google Test Fixtures
Variables in Google Test Fixtures

Time:05-21

Why TEST_F can access the member variable in a class without using any scopes? e.g.,

class ABC : public ::testing::Test
{
protected:
     int a;
     int b;
     void SetUp()
     {      
         a = 1;
         b = 1;
     }

     virtual void TearDown()
     { 
     }
};

TEST_F(ABC, Test123)
{
    ASSERT_TRUE(a == b);

}

why it can directly access a and b, instead of using ABC::a or ABC::b? Does the fixture create a variable for class ABC? if so, should it be ASSERT_TRUE(abc.a == abc.b); instead of ASSERT_TRUE(a == b);?

CodePudding user response:

TEST_F is a macro which defines a new class publicly inheriting from the first argument (in this case, 'ABC'). So it has access to all public and protected members of the test fixture class.

You can inspect the source for this macro in the header file to get a better idea of what it's doing.

TEST_F macro, which if you follow the macro path leads you to... GTEST_TEST_ macro

  • Related