# C++ Unit Testing

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.

## Alternatives to CppUnit

I can think of three alternatives (and one honorable mention):
1. DIY. Just a function called 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 check function 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.
2. John Cricket's C++ TestingFrameWork. The C++ TestingFrameWork used by Crickett Software Limited, this is currently under development, along with an article on unit testing, and the development of this framework. Lightweight, but requires the Standard C++ Library and namespaces.
3. CppUnitLite. After inflicting CppUnit upon us (just kidding, I've used it for some time and it wasn't that bad), Michael Feathers wanted to show that he could do better and produced CppUnitLite. Really lightweight. Nice use of macros. Recommended.
4. cxxtest. Erez Volk's testing framework is different. He's written it so that no fancy C++ features are required (RTTI, exceptions,...). It requires Perl though. From what I've seen the main function is actually generated using a script and includes your test cases straight into the generated file. This strikes me as highly non-scalable, but it might be OK if you generate separate executables for each class under test. As I said: different.

## Trapping Win32 Access Violations in CppUnit

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.

SeException.h

TestCase.cpp

## Related Stuff

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.