|Ernesto Guisado's Website » Programming » Extreme Programming » C++ Unit Testing||Articles | Miscellanea ||
Kent Beck is the author of the Smalltalk unit testing framework (xUnit). This framework has been implemented for most languages. See the XProgramming web site for the full list. Erich Gamma ported it to Java and Michael Feathers ported it to C++. See this link for more information.
To be honest, while I quite like xUnit, I've found that CppUnit is to heavyweight for my taste. A test framework should be simple. Otherwise you spend more time writing and tweaking it, than you spend actually writing tests.
check(test, assert, verify are other popular names) that prints out a message and aborts the test app (or alternatively throws an exception) is all you need. This is consistent with the idea of not spending too much time on the framework and focusing on the actual test cases. The
checkfunction gives you consistent output (something like
Check failed: 0, file D:\bar\testing\foo.cpp, line 5), but you also need some additional automation if you work on a large scale project. You'll need some kind of batch file or Perl script to run sets of test executables and report the results.
During the time I spent using CppUnit, I added some code to it to map Win32 access violations into exceptions that can be trapped by the framework. That way you get a failing test instead of a crash.
There are two code fragments. The first one is the file SeException.h and may go into the same directory than CppUnit. It implements the mapping between Win32 structured exceptions and C++ exceptions. The second one is a modified TestCase.cpp that handles the new exception type.
The best way to integrate unit testing into your C++ project is to make running the tests part of your Daily Build process.
An attempt to show that you don't need a testing framework.
Test Infected. Beck and Gamma's guide to JUnit.
The JUnit site has several articles on test first programming and test driven design.