This post contains a skeleton showing how to implement a simple
unittest using the great boost unit test library, without going all
the way, i.e. having to use bjam or writing your own testrunner.
This use case doesn't seem to be documented (clearly) anywhere in the
great docs of boost::test, so here it is, for everyone who - like me - couldn't find a recipe for this.
First of all, we'll need something to run the tests; this is made
with
the following lines of C++:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
Next, we'll need a test. A simple example:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test)
BOOST_AUTO_TEST_CASE(test_test) {
BOOST_CHECK_EQUAL(1, 1);
}
BOOST_AUTO_TEST_SUITE_END()
Note the difference in defined flags - the testrunner has the flag
BOOST_TEST_MAIN, whereas the test
itself has BOOST_TEST_NO_MAIN.
Finally, we'll need to compile this. On my system with macports, the
following Makefile produces a nice executable:
CXXFLAGS=-I/opt/local/include
LDFLAGS=-L/opt/local/lib
all: test_all
test_all: testrunner.o test_test.o
$(CXX) -o $@ $^ $(LDFLAGS) -lboost_unit_test_framework
Running this yields:
bash-3.2$ ./test_all
Running 1 test case...
*** No errors detected
Mission accomplished!
Enjoy, and kill them bugs.