[Boost.Test] Unable to perform init at startup
(Apologies for the last message, I am using an unfamiliar keyboard and accidentally pressed the 'send' keyboard combination.) Hi all, I'm having a bit of a problem trying to figure out how to perform some class init while manually registering my tests. I want to run a set of tests which all share the same class instance (this is a factory class that creates the objects to be tested.) I am creating a number of global class instances and using boost::bind in the constructor to register the tests with Boost.Test. This works fine, except that I need to create the factory class before the tests can be run. In case the factory class fails, I want to use some of the Boost.Test checks in the initialisation function. However when I do this, I get this runtime error: terminate called after throwing an instance of 'std::runtime_error' what(): can't use testing tools before framework is initialized I am assuming this means I need to wait with the construction of my factory class until Boost.Test is ready, but I am unsure how to do this. I am #defining BOOST_TEST_MODULE, and when I try to declare a function called init_unit_test() as suggested in the docs it tells me the function already exists. If I remove the BOOST_TEST_MODULE #define, then it complains that main() is not implemented. I am a bit confused because I do not understand the reason for having init_unit_test() if you cannot implement it. If you choose not to use the Boost auto function then it seems you must implement your own main(), so I would expect your init code would go there, so there is no need for the init_unit_test() function? Is there some other way not mentioned in the docs to manually register tests (which are class member functions) but where the class can call Boost.Test functions in its constructor? Many thanks, Adam.
terminate called after throwing an instance of 'std::runtime_error' what(): can't use testing tools before framework is initialized
I am assuming this means I need to wait with the construction of my factory class until Boost.Test is ready, but I am unsure how to do this.
I ended up working around this by creating a wrapper function. This function calls init() once (if it has not already been called) followed by the intended test. I wrap all my test functions in this wrapper, this way it does not matter which test is called first, the init code will always run first. I think there is probably room in Boost.Test for some way of running code during init (which uses BOOST_REQUIRE_* etc) for those situations where factory classes must be created prior to the tests which use them, and when the tests are manually registered. Cheers, Adam.
Adam Nielsen
This works fine, except that I need to create the factory class before the tests can be run. In case the factory class fails, I want to use some of the Boost.Test checks in the initialisation function.
However when I do this, I get this runtime error:
Indeed you can't access Boost.Test facilities before its own initialization is run.
terminate called after throwing an instance of 'std::runtime_error' what(): can't use testing tools before framework is initialized
I am assuming this means I need to wait with the construction of my factory class until Boost.Test is ready, but I am unsure how to do this.
One way or another you need to postpone your assertions till after Boost.Test init.
I am #defining BOOST_TEST_MODULE, and when I try to declare a function
What usage variant of boost.test you employ? static/shared/included?
called init_unit_test() as suggested in the docs it tells me the function already exists. If I remove the BOOST_TEST_MODULE #define, then it complains that main() is not implemented.
It appears you are using shared library. In this case you can define BOOST_TEST_MODULE and BOOST_TEST_NO_MAIN. In this case you can define your own main like this: int BOOST_TEST_CALL_DECL main( int argc, char* argv[] ) { // do my own init return ::boost::unit_test::unit_test_main( &init_unit_test, argc, argv ); } or you can skip BOOST_TEST_MODULE and define both main and init_test_module routines. Would you be using static lib, main will always be present, but init_unit_test will depend on BOOST_TEST_MODULE. HTH, Gennadiy
participants (2)
-
Adam Nielsen
-
Gennadiy Rozental