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 accomplis
When your tested class and your test class are in a different namespace (e.g. your tests are in the global namespace), you may need to forward-declare your test class and to add your namespace prefix in FRIEND_TEST:
// foo.h
#include
class FooTest_BarReturnsZeroOnNull_Test;
// Defines FRIEND_TEST.
class my_namespace::Foo {
...
private:
FRIEND_TEST(::FooTest, BarReturnsZeroOnNull);
int Bar(void* x);
};
// foo_test.cc
using namespace my_namespace;
...
TEST(FooTest, BarReturnsZeroOnNull) {
Foo foo;
EXPECT_EQ(0, foo.Bar(NULL));
// Uses Foo's private member Bar().
}
I know that friend unit tests (or the friendliness in C++ in general) and white-box testing are a controversial subject, but when you work on complex, scientific algorithms, each step of which you need to test and validate, but that you don't want to expose in public (or even protected) interfaces, friend tests appear to me as a simple and pragmatic solution, especially in a test-driven development approach. It is always possible to refactor the code later (or to completely remove white-box tests) if it's against one's religion to use friendliness or white-box testing.