
Ed Johnson wrote:
Gennadiy Rozental wrote:
Is there a way to test the private functions of a class without moving them into public scope during test?
What are some strategies for this?
I was hoping to add "friend class boost::unit_test::test_suite;" to my class but that didn't work. Putting aside "why would you need that" question, simple answer is: define test case as a friend to your class. For example if you want to implement test case as free function:
class Foo { friend void foo_test();
void private_method(); };
void foo_test() { Foo f;
f.private_method(); }
If you need multiple test cases having access to the internals you could define test class and make it friend of your class. Next all your test cases will instantiate test class and run test methods.
Gennadiy
In my tests, it does not appear possible to use the auto_unit_test.hpp macros in a test class that is attempting to be a friend of a testee class. The compiler complains that the test class is trying to access private data. Does anyone know how to make them work together? (Should this be a separate thread, or is it alright to add it to this one?)
Thanks,
Ed
Whoops, it appears to work just fine with auto_unit_test.hpp macros. My mistake. This is certainly a better way to test the private data than the preprocessor definitions strategy I mentioned in my other post. -Ed --Foo.hpp-- class Foo { friend void foo_test(); private: void private_method(); }; --TestFoo.cpp-- #define BOOST_AUTO_TEST_MAIN #include "Foo.hpp" BOOST_AUTO_TEST_SUITE( my_suite ); BOOST_AUTO_TEST_CASE( foo_test ) { Foo f; f.private_method(); } BOOST_AUTO_TEST_SUITE_END();