C++ Unit Testing Framework: A Boost Test Tutorial


Part 2: Using Boost Test

Table of Contents
Part 1: Boost Test crash-course
Part 2: Using Boost Test

Following the crash-course introduction, let's see how to install and use Boost, plus some references.

Installing Boost

Now that you have an idea about the benefit we'll briefly see how to install Boost.

On Windows for Visual C++ the simplest way (and also the way promoted by Boost) is to use Boost from BoostPro Computing.

Here are the full installation step. If the installation setup fails, restart it.

  1. Download the latest Boost installer from BoostPro Computing: http://www.boostpro.com/products/free
  2. Install the setup:
    1. Select whatever mirror you like.
    2. It is advised to checked those checked with a cross below:
      [ ] Visual C++ 8.0 (Visual Studio 2005)
      [X] Visual C++ 9.0 (Visual Studio 2008)
      
      [X] Mulithread Debug, DLL --DLL Debug, required when BOOST_DYN_LINK defined.
      [X] Mulithread, DLL       --DLL Release, required when BOOST_DYN_LINK defined.
      [X] Mulithread            --LIB (for CRT DLL) Release, default in Release.
      [X] Mulithread Debug      --LIB (for CRT DLL) Debug, default in Debug.
      [X] Mulithread, static runtime       --LIB (for CRT LIB) Release.
      [ ] Mulithread Debug, static runtime --LIB (for CRT LIB) Debug.
      [ ] Single thread, static runtime
      [ ] Single thread Debug, static runtime
    3. Keep the default options on the next screen.
    4. Wait for the download and installation to complete. In case of error, restart.
    5. Say YES to add Boost to the path at the end.
  3. Add Boost to the include and library folders of Visual Studio:
    1. Open Visual Studio
    2. Go to the menu Tools > Options...
    3. Go to Project and Solutions > VC++ Directories
    4. Add to your Include files: C:\Program Files\boost\boost_X_XX_X
    5. Add to your Include files: C:\Program Files\boost\boost_X_XX_X\boost\tr1
    6. Add to your Library files: C:\Program Files\boost\boost_X_XX_X\lib
  4. (optional) Path inclusion: If you use Boost DLLs (rare) you may add C:\Program Files\boost\boost_X_XX_X\lib to your system PATH.
  5. (optional) Documentation shortcut: Create a shortcut to C:\Program Files\boost\boost_X_XX_X\more\index.htm
  6. Done.

Creating a unit test project in Visual C++

You know how to write C++ code using Boost Test and you have Boost installed. Let's see how to create a nice project for unit testing in Visual Studio.

  1. Open your existing project to test.
  2. Go to menu File > Add > New Project...
  3. Create a Visual C++ empty project.
  4. Either add the classes to test so they get compiled, or make a library and add a reference to your other project (better). You probably also want to have the same StdAfx.h.
  5. In the project settings Build Events > Post-Build Eventdefine:
    Command Line: "$(TargetPath)" --result_code=no --report_level=no --catch_system_error=yes
  6. Include your runner and other test cases.

If you generated a library from your code, you probably have at least third project that will use this library to make the application. You can mark the unit test to be built first, this way you'll always have unit tests checks done each build.

Official documentation

You can find the official documentation here. The documentation may be hard to read, so here are some tips to find your things in it:

  • The BOOST_CHECK and other commands: Boost Test Library > The Unit Test Framework > Testing tools
  • Runner command-line options: Boost Test Library > The Unit Test Framework > Runtime configuration > Parameters reference

Conclusion

Boost Test is now simple to use (once you found your way through their documentation or after reading this article) and provides from minimal tests to complex scenarios. You can find some more advanced features like function base test cases. I hope Boost has now been demystified for you; it's now my favorite C++ testing framework.

— Werner BEROUX, 30 October 2007 (last modified on 2009-06-30)

Parts: Previous1, 2