Open Source Testing Tools Boost Test Framework
Recently I've been coding some C++ to carry out some extensive number crunching for portfolio optimisation. The numbers are mission critical and as such they need to be correct to within a tolerance of my choosing. Unit testing is a fantastic way to ensure that these numbers are correct for all edge cases that can be dreamed up.
I naturally lean towards open source test tools, as I find that free automated testing tools are often well-supported, have good documentation and are easy to pick up. There are many C++ unit testing frameworks but I chose the open source Boost Test Framework because it met all three criteria. I found it simple enough to pick up in about 30 minutes and was coding testing suites within an hour. Read on for the rest of the Boost unit test tutorial.
Installation
I run Ubuntu Karmic (9.10) Server and hence the installation steps are for that distribution. I utilised the pre-built binaries using Apt, but it is straightforward enough to compile the Boost Test Framework from source code if you have met the dependencies. Installing from Apt is as simple as running:
Compilation
The Boost.Test framework is not actually a set of headers which can be included, but has a runtime component which must be linked to when compiling. It is also necessary to include the definition of BOOST_TEST_DYN_LINK before any oher Boost.Test headers are included in the code.
Let's utilise g++ to compile an example single file, test.cpp, which we'll create in a minute. The syntax used is as follows:
Notice that it is linking against boost_unit_test_framework-mt. The "-mt" is important as it includes multithreaded support. Without the "-mt", the compilation does not tend to work. I've also set it to create an executable called "test" with the -o flag.
Unit Test Example
Here's a first unit test example:
Notice that the module has been given the name "Test". This can be changed to something more appropriate, but it will suffice for now. The "unit_test.hpp" header is also included. The BOOST_AUTO_TEST_CASE macro is used to automatically register a test called "SquareTestPositive" to be carried out on execution. An additional macro, BOOST_REQUIRE, ensures that if the square of 2.0 is not 4.0, then an exception is thrown. There are varying levels of strictness - CHECK, REQUIRE and WARN - which can be applied to these macros. A list of them can be found at the UTF testing tools reference.
The next step is to compile the code and run it. The test passes, as it should.
However, if we modify the function so that it produces the cube of x instead of the square, w can see the same test fail.
Creating a Test Suite
Eventually, adding more and more tests will be cumbersome from an organisational point of view. It is time to group them into "test suites". The simplest way to achieve this is to subdivide a test module with additional macros BOOST_AUTO_TEST_SUITE(...) and BOOST_AUTO_TEST_SUITE_END(), where ... is the name of your test suite. Here is an example:
Conclusion
Although this is a simple introduction to open source testing tools, in particular the Boost unit test framework, it will allow you to start designing robust code from the start. Perhaps the next step in the process is to begin adding "fixtures" wich are used for common setup and teardown of data. Boost.Test supports them and the documentation for fixtures can be found at the Boost Test Suite Level Fixture Guide.
