I've got a boost test case. Most lines of this test case are executed regardless of the parameters. But there are parts which are executed based on the provided parameter. I want to avoid writing two separate test cases which are almost identical except in some minor parts. So I need to use something like the following approach to create parameterized test cases:
BOOST_FIXTURE_TEST_CASE(caseA, Fixture)
{
TestFunction("parameterA");
}
BOOST_FIXTURE_TEST_CASE(caseB, Fixture)
{
TestFunction("parameterB");
}
void TestFunction(string param)
{
// ...
// lots of common checks regardless of parameters
// ...
if(param == "parameterA")
BOOST_CHECK(...);
else if(param == "parameterB")
BOOST_CHECK(...);
}
Is there any other way to achieve my goal in a more convenient way? I could find BOOST_PARAM_CLASS_TEST_CASE macro but I am not sure if it's relevant in this case.
Check out BOOST_DATA_TEST_CASE which does exactly what you need. New in boost 1.62.
(This question is a little old, but was the first hit when I searched for how to do this)
No Boost support AFAIK, so I do this:
void test_function(parameters...)
{
<test code>
}
BOOST_AUTO_TEST_CASE(test01) {
test_function(parameters for case #1)
}
BOOST_AUTO_TEST_CASE(test02) {
test_function(parameters for case #2)
}
You can do it with templates if you like them:
template<int I, bool B>
void test_function()
{
for(int i=0; i<I; i++)
if (B) BOOST_REQUIRE(i<10);
}
BOOST_AUTO_TEST_CASE(test01) {
test_function<10, true>();
}
BOOST_AUTO_TEST_CASE(test02) {
test_function<20, false>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With