Search code examples
c++inheritancetestinggoogletestprotected

Testing protected member with googletest


I'm confused regarding inheritance when googletesting. I have a class A that has protected attributes. If I want to access those i have to extend that class, but at the same time i also need to extend public ::testing::Test for the sole purpose of gtest.

What is the most elegant solution to this problem? Also I'm trying to avoid #define protected public


Solution

  • There is a FRIEND_TEST declaration, which is used in the header of tested class. Basically it defines the test as a friend of the class. In my use case, we disable all test includes when compiling in RELEASE mode, so it doesn't do any harm the real executable.

    Have a look at this