The code snippet below is the ordinary declaration for my unit test code, this declaration form will be executed whenever the code were run.
BOOST_AUTO_TEST_CASE(TL_1)
{
...
}
Then later I added some spice to tell the framework whether the unit test case should be executed or not. Following code snippet is the trick I have done for this purpose.BOOST_AUTO_TEST_CASE(TL_1, *boost::unit_test::enable_if<false>())
{
...
}
In my case, this would be a great help. I don't need to wait for all unit test cases are finished in order to see the result. Some more there is only one or two test cases I can really focus on for the same tasks at the same time. Now, as my unit test code are expanding, I need more flexibility and the ability to group relevant test cases together in one source file, and then I can decide which test code to run. Here is the code snippet for the solution.BOOST_AUTO_TEST_SUITE(testlab, *boost::unit_test::label("testlab"))
BOOST_AUTO_TEST_CASE(TL_1, *boost::unit_test::enable_if<false>())
{
...
...
}
BOOST_AUTO_TEST_SUITE_END()
With this code, the test case are grouped under the test suite named testlab. When the code is run, all test cases under this suite will be execute. But there is one flaw in this code, the enable_if() is not working. The test will continue to execute even though it is false. Not only that, neither disabled() and enabled() are working. Luckily precondition() comes to a rescue. The code snippet below tells the framework to continue to execute the test case.struct skipTest
{
bool skip;
skipTest(bool s) : skip(s){}
boost::test_tools::assertion_result operator()(boost::unit_test::test_unit_id)
{
if( skip == false )
return true;
else
return false;
}
};
BOOST_AUTO_TEST_CASE(TL_1, *boost::unit_test::precondition(skipTest(false)))
{
...
...
}
As of now, the test suite will help me to control which group of test case to execute and precondition() will allow me to choose which test case should skip whenever I want during the development.
No comments:
Post a Comment