Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Static Library

To build Boost.Test as a static library, first build the boost build tools, as described in the Getting Started documentation. After you have bootstrapped the build tools, build the test library with by invoking b2 with arguments similar to the following:

b2 --with-test link=static

This will place the resulting libraries in stage/lib at the top level of the boost tree.

In a static library configuration, Boost.Test is compiled into a single static library named boost_unit_test_framework and the test executable is linked against that library. Exactly one compilation unit in the test executable should define BOOST_TEST_MAIN, in order run the tests.

// In exactly one source file
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

// test cases (optional)

Additional source files simply include the main unit test header and define more test cases and/or suites.

// In additional source files
#include <boost/test/unit_test.hpp>

// test cases

To compile the library with no implementation of main, add BOOST_TEST_NO_MAIN to the cxxflags property for b2:

b2 --with-test link=static define=BOOST_TEST_NO_MAIN

When the library is compiled with BOOST_TEST_NO_MAIN defined, one source file must provide a definition for main. The simplest is to delegate to unit_test_main and provide an implementation of an initialization function that manually adds test cases and suites to the test tree. Any test cases or test suites added with BOOST_AUTO_TEST_CASE, BOOST_FIXTURE_TEST_CASE, BOOST_AUTO_TEST_SUITE or BOOST_FIXTURE_TEST_SUITE are automatically added.

// In exactly one source file
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>

boost::unit_test::test_suite *init_function(int argc, char *argv[])
{
    // create test cases and suites and return a pointer to any enclosing
    // suite, or 0.
    return 0;
}

int main(int argc, char *argv[])
{
    return boost::unit_test::unit_test_main(init_function, argc, argv);
}

Example Source Code

PrevUpHomeNext