I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members.
How to accomplish that?
I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members.
How to accomplish that?
I know this is old but I was searching for the same answer today. "gtest_prod.h" just introduces a simple macro to reference test classes.
So
FRIEND_TEST(FooTest, BarReturnsZeroOnNull);
is equivalent to:This works because each test is its own class as mentioned in the previous answer.
A far better strategy is to not allow friend tests among your unit tests.
Allowing friend tests accessing private members will lead to a code base that is hard to maintain. Tests that break whenever a component's inner implementation details are refactored is not what you want. If extra effort is instead put into getting a design where components can be tested through their public interface, you will get tests that only need updating whenever the public interface of a component is updated.
Tests relying on
gtest/gtest_prod.h
should be seen as a sign of poor design.Try this (straight from Google Test docs...):
For example: