C++ Unit Testing With Boost.Test
(permanent link: http://web.archive.org/web/20160524135412/http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/)
The above is a brilliant article and better than the actual Boost documentation.
Edit:
I also wrote a Perl script which will
auto-generate the makefile and project
skeleton from a list of class names,
including both the "all-in-one" test
suite and a stand alone test suite for
each class. It's called
makeSimple and can be downloaded
from Sourceforge.net.
What I found to be the basic problem is that if you want to split your tests into multiple files you have to link against the pre-compiled test runtime and not use the "headers only" version of Boost.Test. You have to add #define BOOST_TEST_DYN_LINK
to each file and when including the Boost headers for example use <boost/test/unit_test.hpp>
instead of <boost/test/included/unit_test.hpp>
.
So to compile as a single test:
g++ test_main.cpp test1.cpp test2.cpp -lboost_unit_test_framework -o tests
or to compile an individual test:
g++ test1.cpp -DSTAND_ALONE -lboost_unit_test_framework -o test1
.
// test_main.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
// test1.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test1_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(2<1);
}
BOOST_AUTO_TEST_SUITE_END()
// test2.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test2_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(1<2);
}
BOOST_AUTO_TEST_SUITE_END()