Friend class

1,342 views
Skip to first unread message

roverken

unread,
Oct 13, 2009, 3:15:33 PM10/13/09
to Google C++ Testing Framework
Hi

I am trying to test private methods by setting my test fixture as a
friend of the class with the private methods, however for each test in
the test fixture google test creates a new class which must also be
set as a friend.

Is there any easier way to do this where I can set the whole test
fixture as a friend somehow?

Thanks

Josh Kelley

unread,
Oct 13, 2009, 4:06:34 PM10/13/09
to roverken, Google C++ Testing Framework
Because Google Test implements tests as subclasses and C++ doesn't permit extending friendship to an entire class hierarchy, there's no prepackaged way of doing this.  However, there are several techniques you can use that are a lot easier than making each test a friend.  The Google Test Advanced Guide has more details:

http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Testing_Private_Code

--
Josh Kelley

Likai Liu

unread,
Oct 16, 2009, 6:36:47 PM10/16/09
to Josh Kelley, roverken, Google C++ Testing Framework
I personally like to use a thin derivative class like this:

class Foo {
friend class FooForTesting;
};

class FooForTesting : public Foo {
public:
// exposes otherwise private/protected members and methods in Foo.
};

Rather than testing Foo, you will test FooForTesting instead. The
FooForTesting class makes it obvious which internal parts of Foo you
are relying on for the tests.

liulk

Reply all
Reply to author
Forward
0 new messages