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.

Advertisements
This entry was posted in c/c++ and tagged , , , . Bookmark the permalink.

3 Responses to Testing C++ Applications using CppUnit

  1. swinefeaster says:

    Do you know any way of plugging this into Maven?

    thanks

  2. mafr says:

    You want to run a C++ build from maven? The only idea that comes to mind is to use the antrun plugin (http://maven.apache.org/plugins/maven-antrun-plugin/) to execute the Makefile. Ant’s exec task is capable of executing arbitrary commands.

    Can you go into a bit more detail why you want to do this and what exactly you want to achieve?

    Cheers,
    Matthias

  3. Krok says:

    Maven is a project management tool which includes building. It’s not necessarily Java specific.

    I recommend you look at the nar-maven-plugin. If things proceed it’s foreseeable we’ll have the power of maven dependencies, deployment,distribution, reporting, site generation etc for CPP projects :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s