The Boost.Test documentation and examples don't really seem to contain any non-trivial examples and so far the two tutorials I've found here and here while helpful are both fairly basic.
I would like to have a master test suite for the entire project, while maintaining per module suites of unit tests and fixtures that can be run independently. I'll also be using a mock server to test various networking edge cases.
I'm on Ubuntu 8.04, but I'll take any example Linux or Windows since I'm writing my own makefiles anyways.
Edit
As a test I did the following:
// test1.cpp
#define BOOST_TEST_MODULE Regression
#include <boost/test/included/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
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test2_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(1<2);
}
BOOST_AUTO_TEST_SUITE_END()
Then I compile it: g++ test1.cpp test2.cpp -o tests
This gives me about a bazillion "multiple definition of" errors during linking.
When it's all in a single file it works fine.
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()