The Problem
There are many Unit Testing Frameworks (UTF) for C++. Wikipedia list over 50 of them. Not all of them are created equal. CPPUnit is one of the oldest and is still popular. Google Test has a strong brand name backing. Boost Unit Testing framework is another strong brand even if it is a non-profit organisation. Although a bit dated, over eleven years old, Noel Llopis gives a good description of what to look for in a unit test framework. The major difference between Boost and the other popular unit test frameworks is that there is no explicit ‘setup’ or ‘teardown.’ It is all done through RAII.
Visual studio also provides a native unit testing framework. It uses .NET in the background to run the tests. The user interface is rather nice. It is possible to run specific tests by just selecting the individual tests using a mouse click. You could run just the failed tests or debug only one failed test. Trust Microsoft to produce one of the best user experiences for the developer.
But being Windows specific it does not rank high with regard to portability to other Operating Systems (OS). However Microsoft now provides a means of incorporating Boost Unit Testing within Visual Studio. This is rather excellent news for those of use using Open Source Software (OSS) on Windows. Here I will discuss how to use Boost UTF on Windows.
Install Boost
Download and install Visual Studio 2105. The Express edition is free for non-commercial use. Download and unzip Boost in some folder say c:\Boost. Now run the Visual Studio command prompt from the start menu and ‘cd c:\Boost\boost_1_59_0’ or whatever be your version of boost. Run ‘bootstrap’. This will create bjam.exe. Now run:
bjam –-with-test variant=release link=shared address-model=64
‘address-model=64’ is required if you are building 64-bit DLL. This will create two files:
• boost_unit_test_framework-vc140-mt-1_59.dll and
• boost_unit_test_framework-vc140-mt-1_59.lib.
vc140 indicates the compiler and its version. Copy the DLL to a folder in your Path environment variable.
Using Visual Studio
Download the Boost test adaptor from MSDN and install it by double-clicking on it. Now launch or re-launch Visual Studio. Create a new project using File\New\Project\Visual C++\Test\Boost Unit Test
. Accept the defaults. I am not sure why it creates two files. Retain just BoostUnitTestSample. Use the code listed later(adapted from Boost). To build you need to:
- Include your Boost include folder which in our case would be c:\Boost\boost_1_59_0\boost to the list of include fodders;
- Add the stage\lib to your library folder which would be c:\Boost\boost_1_59_0\boost\stage\lib;
- Add BOOST_TEST_DYN_LINK to your pre-processor definitions; and
- Ensure that is selected in Project Properties\General\Platform
Build the application. If you are using other Boost libraries the linker might complain about ‘libboost_something_or_other.lib’ not found. In that case you will have to include BOOST_ALL_NO_LIB to your pre-processor definitions and manually list each of the boost libraries in ‘additional dependencies.’ When the build is complete run the tests from ‘Test\Run\All tests’. Et voila you have now built your first Boost Test on Windows.
The Code
#include "stdafx.h" int add(int i, int j) { return i + j; } int sub(int i, int j) { return i - j; } BOOST_AUTO_TEST_CASE(TestAdd) { // seven ways to detect and report the same error: BOOST_CHECK(add(2, 2) == 4); // #1 continues on error BOOST_REQUIRE(add(2, 2) == 4); // #2 throws on error if (add(2, 2) != 4) BOOST_ERROR("Ouch..."); // #3 continues on error if (add(2, 2) != 4) BOOST_FAIL("Ouch..."); // #4 throws on error if (add(2, 2) != 4) throw "Ouch..."; // #5 throws on error BOOST_CHECK_MESSAGE(add(2, 2) == 4, // #6 continues on error "add(..) result: "<< add(2, 2)); BOOST_CHECK_EQUAL(add(2, 2), 4); // #7 continues on error } BOOST_AUTO_TEST_CASE(TestSubtract) { // seven ways to detect and report the same error: BOOST_CHECK(sub(2, 2) == 0); // #1 continues on error BOOST_REQUIRE(sub(2, 2) == 0); // #2 throws on error if (sub(2, 2) != 0) BOOST_ERROR("Ouch..."); // #3 continues on error if (sub(2, 2) != 0) BOOST_FAIL("Ouch..."); // #4 throws on error if (sub(2, 2) != 0) throw "Ouch..."; // #5 throws on error BOOST_CHECK_MESSAGE(sub(2, 2) == 0, // #6 continues on error "sub(..) result: " << sub(2, 2)); BOOST_CHECK_EQUAL(sub(2, 2), 0); // #7 continues on error }
Pingback: Machine Learning with C++ MLPack on Windows | The Sunday Programmer
You ate one dash in:
bjam –with-test variant=release link=shared address-model=64
should be:
bjam –-with-test variant=release link=shared address-model=64
b.regards!
Thanks… for pointing it out.
Now You can be sure that someone is reading it 🙂
I normally cut and paste commands. I am surprised this occurred.