Notwithstanding the comments regarding the appropriateness of testing private methods, suppose you really need to... this is often the case for example when working with legacy code prior to refactoring it into something more appropriate. Here is the pattern i've used:
// In testable.hpp:
#if defined UNIT_TESTING
# define ACCESSIBLE_FROM_TESTS : public
# define CONCRETE virtual
#else
# define ACCESSIBLE_FROM_TESTS
# define CONCRETE
#endif
Then, within the code:
#include "testable.hpp"
class MyClass {
...
private ACCESSIBLE_FROM_TESTS:
int someTestablePrivateMethod(int param);
private:
// Stuff we don't want the unit tests to see...
int someNonTestablePrivateMethod();
class Impl;
boost::scoped_ptr<Impl> _impl;
}
Is it better than defining test friends? It seems less verbose than the alternative, and is clear within the header what is happening. Both solutions have nothing to do with security: if you are really concerned about the methods or members, then these need to be hidden inside an opaque implementation possibly with other protections.