building/running boost unit test framework

#include <boost/test/unit_test.hpp> using namespace boost::unit_test; void free_test_function() { BOOST_CHECK(2 == 1); } test_suite* init_unit_test_suite( int, char* [] ) { framework::master_test_suite().p_name.value = "Unit test example 02"; framework::master_test_suite().add( BOOST_TEST_CASE( &free_test_function ), 2 ); } Im trying to build above program, but facing linking errors. Linker can not find main When I add #define BOOST_TEST_MAIN 1 before including unit-test.cpp, it gives me compilation error "redefinition of init_unit_test_suite" When I define both BOOST_TEST_DYN_LINK & BOOST_TEST_MAIN, then it build successfully but I get error "Test setup error: test tree is empty" Someone please help me solve it. :wq Pankaj

pankaj takawale wrote:
#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;
void free_test_function() { BOOST_CHECK(2 == 1); }
test_suite* init_unit_test_suite( int, char* [] ) { framework::master_test_suite().p_name.value = "Unit test example 02"; framework::master_test_suite().add( BOOST_TEST_CASE( &free_test_function ), 2 ); }
Im trying to build above program, but facing linking errors. Linker can not find main
Did you link with boost_unit_test framework library? The way above test is written requires static library.
When I add #define BOOST_TEST_MAIN 1 before including unit-test.cpp, it gives me compilation error "redefinition of init_unit_test_suite"
Yes BOOST_TEST_MAIN automatically defines test module init function. It's intended to be used to automatically registerred test units. Like this: #define BOOST_TEST_MAIN #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE( free test_function ) { BOOST_CHECK( 2 == 1 ); } You still required to link with the the static library. If you prefer to use shared one add #define BOOST_DYN_LINK on top of the test file.
When I define both BOOST_TEST_DYN_LINK & BOOST_TEST_MAIN, then it build successfully but I get error "Test setup error: test tree is empty"
Dynamic library requires slightly different initialization routines. The easiest way to avoid this hurdle is to use automated registration like in a example above. You can read more about initialization routines and Boost.Test usage variants in beta version of new documentation at: patmedia.net/~rogeeff/html/index.html HTH, Gennadiy
participants (2)
-
Gennadiy Rozental
-
pankaj takawale