Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Test: How to write parameterized test cases

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.

like image 450
B Faley Avatar asked Jan 07 '13 12:01

B Faley


2 Answers

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)

like image 198
Dave Sawyer Avatar answered Oct 12 '22 09:10

Dave Sawyer


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>();
}
like image 32
gatopeich Avatar answered Oct 12 '22 08:10

gatopeich