Boost C++ Libraries Home Libraries People FAQ More


Header Only

The entire unit test framework can be incorporated into an executable by including the file <boost/test/included/unit_test.hpp>. Because the entire implementation is incorporated into the source file including this header, only a single source file in the test executable can include this header.

Additional source files must define BOOST_TEST_NO_LIB before including <boost/test/unit_test.hpp> to prevent any automatic linking to a shared library. If additional source files attempt to include <boost/test/included/unit_test.hpp>, a multiply defined symbol error will occur during linking.

// In exactly one source file:
#include <boost/test/included/unit_test.hpp>

// test cases (optional)

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

// test cases

If BOOST_TEST_NO_MAIN is defined, then an implementation of main must be provided that executes the test cases. This is most readily done by delegating to unit_test_main.

// In exactly one source file
#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

The tutorials all use the header-only version of Boost.Test.