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.
- Download the latest Boost installer from BoostPro Computing: http://www.boostpro.com/products/free
- Install the setup:
- Select whatever mirror you like.
- 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
- Keep the default options on the next screen.
- Wait for the download and installation to complete. In case of error, restart.
- Say YES to add Boost to the path at the end.
- Add Boost to the include and library folders of Visual Studio:
- Open Visual Studio
- Go to the menu Tools > Options...
- Go to Project and Solutions > VC++ Directories
- Add to your Include files: C:\Program Files\boost\boost_X_XX_X
- Add to your Include files: C:\Program Files\boost\boost_X_XX_X\boost\tr1
- Add to your Library files: C:\Program Files\boost\boost_X_XX_X\lib
- (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.
- (optional) Documentation shortcut: Create a shortcut to C:\Program Files\boost\boost_X_XX_X\more\index.htm
- 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.
- Open your existing project to test.
- Go to menu File > Add > New Project...
- Create a Visual C++ empty project.
- 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.
- In the project settings Build Events > Post-Build Eventdefine:
Command Line: "$(TargetPath)" --result_code=no --report_level=no --catch_system_error=yes - 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)