Testing the googletest protected item
I am confused about inheritance when googletesting. I have class A
one that has attributes protected
. If I want to access those I have to extend this class, but at the same time I also need to extend public ::testing::Test
with only purpose gtest
.
What's the most elegant solution to this problem? I also try to avoid#define protected public
There is a FRIEND_TEST declaration that is used in the header of the class under test. He basically defines the test as a friend of the class. In my use case, we disabled all tests when compiling in RELEASE mode, so it won't damage the real executable.
Have a look at this
To avoid leaving test traces in the class under test, use multiple inheritance with a fixture:
class ToBeTested
{
protected:
bool SensitiveInternal(int p1, int p2); // Still needs testing
}
// Google-test:
class ToBeTestedFixture : public ToBeTested, public testing::Test
{
// Empty - bridge to protected members for unit-testing
}
TEST_F(ToBeTestedFixture, TestSensitive)
{
ASSERT_TRUE(SensitiveInternal(1, 1));
ASSERT_FALSE(SensitiveInternal(-1, -1));
}