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
+5
source to share
2 answers
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));
}
+10
source to share