views:

195

answers:

1

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?

+3  A: 

Try this (straight from Google Test docs...):

FRIEND_TEST(TestCaseName, TestName);

For example:

// foo.h
#include <gtest/gtest_prod.h>

// Defines FRIEND_TEST.
class Foo {
  ...
 private:
  FRIEND_TEST(FooTest, BarReturnsZeroOnNull);
  int Bar(void* x);
};

// foo_test.cc
...
TEST(FooTest, BarReturnsZeroOnNull) {
  Foo foo;
  EXPECT_EQ(0, foo.Bar(NULL));
  // Uses Foo's private member Bar().
}
hobbit
What about if I have another test for instance BarReturnsOneOnSth. Do I have to add another FRIEND_TEST declaration for that test too?
pajton
Yes. Each test is technically a class, and you need to befriend them one at a time.
hobbit