getting started with boost test on windows xp

Thu, Jun 18, 2009

I’ve been looking into unit testing for C++ and tried out cppunit, which blows up spectaculary when it runs and the errors are so bad I just can’t be bothered with it. It took and age to figure out how to build it with VisualStudio 9 and it still doesn’t run properly. So I had a look at Boost.Test. The blurb on the site proclaims “one of the most highly regarded and expertly designed C++ library projects in the world” and it is easier to use than cppunit. So how to get started with a simple test?

Download Boost from here and unpack into BOOST_HOME. Then download bjam from here. Download the prebuilt binary and install it somewhere on your PATH. You’ll need it to build the Unit Test Framework library (UTF) and DLL. To do that:

cd BOOST_HOME/boost/libs/test
that will build some libs and DLLs but you’re only interested in these two:

BOOST_HOME\bin.v2\libs\test\build\msvc-9.0\debug\asynch-exceptions-on\boost_unit_test_framework-vc90-gd-1_39.lib BOOST_HOME\bin.v2\libs\test\build\msvc-9.0\debug\asynch-exceptions-on\boost_unit_test_framework-vc90-gd-1_39.dll

What I did next was create a minimal Win32 console application with VisualStudio and put it in HELLO_BOOST_HOME. Then copied various things into it:

copy BOOST_HOME\bin.v2\libs\test\build\msvc-9.0\debug\asynch-exceptions-on
boost_unit_test_framework-vc90-gd-1_39.lib HELLO_BOOST_HOME\lib
and I made boost_unit_test_framework-vc90-gd-1_39.dll available on the PATH. A quicker way to do this is just copy boost_unit_test_framework-vc90-gd-1_39.dll to HELLO_BOOST_HOME\Debug once you’ve build the application. To make sure everything’s working, just copy/paste the example code into your one source file:
#include <boost/test/unit_test.hpp>

int add(int i, int j) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test ) { BOOST_CHECK(add(2,2) == 4);

BOOST_REQUIRE(add(2,2) == 4);

if(add(2,2) != 4) BOOST_ERROR(“Ouch…”);

if(add(2,2) != 4) BOOST_FAIL(“Ouch…”);

if(add(2,2) != 4) throw “Ouch…”;

BOOST_CHECK_MESSAGE(add(2,2) == 4, “add(..) result: “ << add(2,2));

BOOST_CHECK_EQUAL(add(2,2), 4); }

If you want to use a fixture with your test cases you can do this:
#include <boost/test/unit_test.hpp>

class TestFixture { public: TestFixture() : fixtureCount(0) { BOOST_TEST_MESSAGE(“setup fixture”); } ~TestFixture() { BOOST_TEST_MESSAGE(“teardown fixture”); }

public: int fixtureCount; };

BOOST_FIXTURE_TEST_SUITE(suiteName, TestFixture)

BOOST_AUTO_TEST_CASE(test_case1) { BOOST_CHECK(fixtureCount == 1); }

BOOST_AUTO_TEST_CASE(test_case2) { BOOST_CHECK_EQUAL(fixtureCount, 0); }


There are a few things to note. If you leave out #define BOOST_TEST_DYN_LINK, the build will fail as the linker will not be able to find libboost_unit_test_framework-vc90-mt-gd-1_39.lib and if you rename boost_unit_test_framework-vc90-gd-1_39.lib it will build but when you run, you’ll get the horrendous error:

unresolved external symbol _main referenced in function ___tmainCRTStartup    MSVCRTD.lib

as there’s no main(). So make sure you use #define BOOST_TEST_DYN_LINK and to get round the next linker error, rename the lib:

boost_unit_test_framework-vc90-gd-1_39.lib -> boost_unit_test_framework-vc90-mt-gd-1_39.lib

No idea why it doesn’t build it with that name but if you rename it and link to it, it works just fine. Once it’s built, dump boost_unit_test_framework-vc90-gd-1_39.dll in your Debug directory and away you go.

I’m going to apply this lot to GADfly and then work out how to package it. With Java and Maven you don’t have to version control all the JUnit jars. What I’ll prolly do with this is version control the test source and the test exe along with boost_unit_test_framework-vc90-gd-1_39.dll. That way the tests can be run without having to compile them. Of course, my main dev env will have the full Boost source tree so I can update and create new Boost.Test C++ unit tests.

comments powered by Disqus