With cppunit you write a class derived from
CppUnit::TestCase
for each test you want to write and implement the test in
runTest()
. So for every
behaviour your classes exhibit you're going to want a class derived from
CppUnit::TestCase
.
For a first time use of CppUnit I'd try and avoid fixtures - they tend to blur the issue until you're sure about how to write the tests themselves.
Sourceforge[
^] has got some examples of how to use it with and without fixtures. Note that they're using one fixture for each class under test and one TestCase for each behaviour they're testing.
Personally I think CppUnit is rather overcomplicated for what it does. It strains C++ in ways it wasn't meant to go. As an alternative you could always avoid using an off the shelf unit testing framework and just use free functions with some handcrafted asserts.
For example I use:
#define ASSERT_THAT(expr) \
if(!(expr)) error_to_console( #expr, __FILE__, __LINE__ );
where
error_to_console
is implemented as:
void error_to_console( const std::string &error_message,
const std::string &file_name,
long line_no )
{
std::cerr << file_name << "(" << line_no << ") : error : "
<< error_message << std::endl;
}
Then you can write tests like:
void require_string_to_implement_equality()
{
ASSERT_THAT( std::string( "ABC" ) == std::string( "ABC" ) );
}
which to my mind is a lot easier than messing about with CppUnit test cases, tests and fixtures.
It's worth searching out other unit testing frameworks. Michael Feathers, the original author of CppUnit, developed a smaller, less complicated framework called
CppUnitLite[
^]. This probably does everything you want to start out, admittedly without any real documentation. However the code's easy enough to read AND there are plenty of examples on the web.