[test] Running either unit tests or normal program based off of command line switches

As I currently understand boost.test, it provides its own main function. If I want to make a separate executable to run the tests, that seems straight forward. But how would I embed the testing inside a normal program with a simple command-line switch for running them?

Jason House wrote:
As I currently understand boost.test, it provides its own main function. If I want to make a separate executable to run the tests, that seems straight forward. But how would I embed the testing inside a normal program with a simple command-line switch for running them?
It seems that no matter what I do, I keep getting a linking error with my latest attempt to get this to work: /usr/local/lib/libboost_unit_test_framework.so: undefined reference to `init_unit_test_suite(int, char**)' I am using boost 1.33.1. Below is what I consider to be the relevant part of my code that's giving me trouble. Most of it is copy/paste right out of online examples with only minor customization. What's really strange is that I get another unrelated link error. If I comment out my definition of init_unit_test_suite, my link errors reduce (and init_unit_test_suite is the only undefined symbol). If I comment out the unit test code completely, the program compiles and links without problem. I'm really baffled what could be going wrong. int main(int argc, char **argv){ ... boost::program_options::variables_map vm; ... if (vm.count("unit_test")){ // code lifted from boost/test/impl/unit_test_main.ipp::main using namespace boost::unit_test; try{ int argc_alt = 1; // customized char* argv_alt[] = {"housebot.exe"}; // customized framework::init( argc_alt, argv_alt ); // customized framework::run(); results_reporter::make_report(); return runtime_config::no_result_code() ? boost::exit_success : results_collector.results( framework::master_test_suite().p_id ).result_code(); } catch( std::logic_error const& ex ) { std::cerr << "Boost.Test internal framework error: " << ex.what() << std::endl; return boost::exit_exception_failure; } catch( ... ) { std::cerr << "Boost.Test internal framework error: unknown reason" << std::endl; return boost::exit_exception_failure; } } ... } boost::unit_test::test_suite::test_suite* get_connection_test_suite(); boost::unit_test::test_suite::test_suite* init_unit_test_suite( int, char* [] ) { boost::unit_test::test_suite::test_suite* test = get_connection_test_suite(); return test; }

"Jason House"
Jason House wrote:
As I currently understand boost.test, it provides its own main function. If I want to make a separate executable to run the tests, that seems straight forward. But how would I embed the testing inside a normal program with a simple command-line switch for running them?
It seems that no matter what I do, I keep getting a linking error with my latest attempt to get this to work: /usr/local/lib/libboost_unit_test_framework.so: undefined reference to `init_unit_test_suite(int, char**)'
I am not sure what's going on. Try what I recomended in other post. Make sure main.o (or whatever it's named) contains init_unit_test_suite (using nm++ or similar). If it still failing try to write standalone example with Boost.Test calls only and post it here. Gennadiy

"Jason House"
As I currently understand boost.test, it provides its own main function. If I want to make a separate executable to run the tests, that seems straight forward. But how would I embed the testing inside a normal program with a simple command-line switch for running them?
Depends how you build UTF:
1. Static library/"included" version
1.a. define BOOST_TEST_NO_MAIN during library/executable build
1.b. in your main file call unit_test_main():
if( /* call unit test */ )
return boost::unit_test::unit_test_main( argc, argv );
2. Dynamic library
2.a. define BOOST_TEST_NO_MAIN during library build
2.b. define BOOST_TEST_DYN_LINK during both library and executable build
2.c. In the file containing function main() include

Which version of boost test does this apply to? I downloaded the latest stable boost version of 1.33.1 and grep -r gives no matches for BOOST_TEST_NO_MAIN or BOOST_TEST_DYN_LINK. I also see no definitions for a function named unit_test_main. There's a header file with that name, but it defines a normal main regardless of what is #define'd. I suspect that maybe this is a feature with the CVS version? Gennadiy Rozental wrote:
"Jason House"
wrote in message news:edhfuf$dth$1@sea.gmane.org... As I currently understand boost.test, it provides its own main function. If I want to make a separate executable to run the tests, that seems straight forward. But how would I embed the testing inside a normal program with a simple command-line switch for running them?
Depends how you build UTF:
1. Static library/"included" version 1.a. define BOOST_TEST_NO_MAIN during library/executable build 1.b. in your main file call unit_test_main():
if( /* call unit test */ ) return boost::unit_test::unit_test_main( argc, argv );
2. Dynamic library 2.a. define BOOST_TEST_NO_MAIN during library build 2.b. define BOOST_TEST_DYN_LINK during both library and executable build 2.c. In the file containing function main() include
and define BOOST_TEST_MAIN 2.d. in your main file call unit_test_main(): if( /* call unit test */ ) return boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
Gennadiy

"Jason House"
Which version of boost test does this apply to? I downloaded the latest stable boost version of 1.33.1 and grep -r gives no matches for BOOST_TEST_NO_MAIN or BOOST_TEST_DYN_LINK. I also see no definitions for a function named unit_test_main. There's a header file with that name, but it defines a normal main regardless of what is #define'd.
I suspect that maybe this is a feature with the CVS version?
I think you are right. With 1.33.1. You will probably need to exclude unit_test_main.cpp from build. Gennadiy
participants (2)
-
Gennadiy Rozental
-
Jason House