if Jeff Atwood and Mr. T can not convince you of the benefits of unit testing then I don’t know who else can. So I won’t press the issue of writing test cases before coding or unit testing at all. Instead, as you are reading this post, I am assuming that you are not “the fool” being pitied upon by Mr. T and actually consider writing test cases a coding prerequisite.

Now that we have agreed upon (at least I am assuming we have) the importance of unit testing, we’ll come to the title of this post “Unit Testing with Boost.Test”. Why Boost.Test (or any other testing framework for that matter)? You can very well write test cases using `assert` macros but this approach is very trivial and rigid and does not lend itself to flexibility and versatility in the writing of test cases (as we shall see in the next part of this post). These limitations render the `assert` macro unsuitable for more complex projects. Boost.Test is an easy to use and flexible way to write use cases and is part of the boost c++ libraries which is a set of standards compliant, cross platform c++ libraries.

In this post I will show you how to install the boost libraries. You can download a version appropriate for your platform from http://www.boost.org/users/download/#releases and extract to a folder in a common location (e.g. /usr/local/include/boost). If you’d rather checkout an svn snapshot, you can do an svn checkout by issuing following command in a shell:
$ svn co http://svn.boost.org/svn/boost/trunk boost-trunk
Please visit http://www.boost.org/users/download/#repository for more instructions.
If you are using Ubuntu, you can also do
$ sudo apt-get install libboost-test-dev
But I don’t recommend this approach as the packages are somewhat out of date.

I will be using version 1.45.0 of boost libraries for this article which is the latest at the time of this writing. if you have any previous version installed, please check the changelog to see if you need to upgrade to this version. eventhough its always best to use latest release build of any library, some of your projects might break with new changes so be carefull to read the changelog carefully before upgrading.

If you need more info about installing boost libraries, please visit this page if you are on *nix or go here if you are on Windows platform.

Now that we have installed boost libraries, we need to set up our development environment but before that there is something else that we need to take care of. The boost libraries are mostly header only. That is, they consist entirely of header files containing templates and inline functions and require no separately-compiled library binaries but there are some libraries that must be built separately and then there are some libraries that can _optionally_ be compiled separately. Boost.Test is of the latter form. You can use it as a header only library or you can pre-compile it and link your project to this binary. It is recommended that this library be used as a separately compiled binary as it will greatly reduce the compilation time for your project.

Building the Boost.Test library is not very complicated. Boost has its own build system which is accessible from the boost installation directory. To build Boost.Test perform following steps:
`cd` to the boost installation directory (assuming your installation directory is /usr/local/include/boost/trunk)
$ cd /usr/local/include/boost/trunk
To see all available options
$ ./bootstrap.sh --help
To see a list of all libraries that need to be / can be built do
$ ./bootstrap.sh --show-libraries
You probably only want to build Boost.Test so type following in a shell
$ ./bootstrap.sh --with-libraries=test
When this command successfully finishes execution you will get a message indicating that bootstrapping has been successfully done. Next you will actually build the library. Type following on the shell
$ ./bjam
Once `bjam` finishes successfully, it will place the library binaries in $BOOST_HOME\stage\lib directory.

Verify the existance of `libboost_unit_test_framework.a` and `libboost_unit_test_framework.so` in that directory. Your Boost.Test installation is ready. In the next post we will set up a sample project and look at how we can use Boost.Test in our projects to benefit from this great testing framework.

Share