Have you looked at the
cook book or do you mean how to write good unit tests?
If you are looking for good examples of writing tests then ignore what you see in the cook book. For example the first test at the top of the page is awful.
Code:
class ComplexNumberTest : public CppUnit::TestCase {
public:
ComplexNumberTest( std::string name ) : CppUnit::TestCase( name ) {}
void runTest() {
CPPUNIT_ASSERT( Complex (10, 1) == Complex (10, 1) );
CPPUNIT_ASSERT( !(Complex (1, 1) == Complex (2, 2)) );
}
};
function name: runTest
What test!! You should always give a unit test a good name to help in identifying the problem in the compiler output, or xml if that is the way you are inclined yet this defeats TDD. If you can not think of a good name then it is generally a test smell that it is doing too much.
I like
Roy Osherove's naming convention
Quote:
methodUnderTest_scenario_expectedResult
|
Generally only have one assert per test. A unit test has three actions:
If the above example worked on the same instance then compare object types. Note some times these action are referred to using different names and acronyms.
Magic numbers. What is so special about 10,1,2 ? Use constants or if you must use 0 and 1 to help show to a user they have no special value.
Here is a kata cast I did after a couple of tries, there are mistakes in it yet you may learn something from watching it.