Testing C++ Applications using CppUnit

Unit testing is hip these days and every programming language has its own JUnit clone that mimics the original more or less closely. For C++, there's the excellent CppUnit package that I've been using extensively lately. Unfortunately, C++ is less dynamic than languages like Java, so you can't simply tell the framework to execute all methods who's names start with "test". With CppUnit and C++, you have to write a fair amount of boiler plate code yourself to register test cases and their methods.

To get you started, I published my Instant-CppUnit package. All you have to do is to extract it in your project's root directory and adjust the Makefile that comes with it. The package includes a template with examples, a test runner (taken from CppUnit's documentation), a dynamic Makefile and some instructions. To execute the test suite, type make test. New tests can simply be placed into the directory and will be picked up by the Makefile and built automatically. I use the convention to name test files after the classes being tested. All .cpp/.h-files starting with test_ belong to the test environment.

If you have never worked with CppUnit before, I recommend The CppUnit Cookbook, which provides an excellent introduction.

social