Unit tests are usually run in a separate executable to your application.
To run the boost
unit test library the test executable must link to boost_test_exec_monitor
.
Note: the precise name of a boost
library depends on your operating system, compiler, multi-threading and debug modes. See: how can I decode boost library naming.
Boost library naming is usually simpler on linux
. However, if you define the relevant environment variables then the following qmake
lines will link on Windows with MinGW
in multithreaded dubug mode:
BOOST_LIB_SUFFIX = $${MINGW_VERSION}-mt-gd$${BOOST_VERSION}
INCLUDEPATH += $$BOOST_ROOT
LIBS += -L$${BOOST_ROOT}/stage/lib
LIBS += libboost_test_exec_monitor$${BOOST_LIB_SUFFIX}
You could put all of your unit tests in a single file, but it's better to put the following in a file called test_main.cpp
(for example) and add it to the source files in your qmake
file.
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
Separate unit test files can then be written and added to the unit test qmake
file, e.g.:
#include "Hero.hpp"
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(MyTests)
BOOST_AUTO_TEST_CASE( my_test ){
Hero TestHero ("Hika",100,100,100,100,100,100,100,0,100);
BOOST_CHECK_EQUAL (100, TestHero.getHP()); }
BOOST_AUTO_TEST_SUITE_END()
Your unit test qmake
file should then include the test files together with the relevant source files from your application, e.g.:
SOURCES += tests/test_main.cpp \
tests/my_test.cpp \
src/Hero.cpp
Look in the tests
directory at: via-httplib for a complete example.