Search code examples
c++unit-testingboostboost-test

Repeat a Boost unit test with different class type


I have two classes which share the exact same API and functionality (they are wrapping different 3rd-party APIs to provide same functionality). The two classes do not have a common base-class/interface.

I have a boost unit test for one of them and would like to run the same exact tests on the other, but right now I only know how to copy-paste the test and find/replace the class name. As well as being annoying having to update tests in two places, it also means there is no guarantee the two classes are being tested identically.

Is there a way I can 'template' a test-case? If not, how would you solve this? All I can think of so far is something like (excuse the pseudo code):

template<class T>
void runTests()
{
 T t;
 //do tests here
}

BOOST_AUTO_TEST_CASE(test_X)
{
 runTests<X>();
}
BOOST_AUTO_TEST_CASE(test_Y)
{
 runTests<Y>();
}

But I don't even know if this would work.


Solution

  • It's perfectly OK, why not? However, there is template test-case in boost

    http://www.boost.org/doc/libs/1_54_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-case-template.html

    So, something like this can help

    typedef boost::mpl::vector<X, Y> XY_types;
    BOOST_AUTO_TEST_CASE_TEMPLATE(test_X_or_Y, T, XY_types)
    

    and test will be called twice, first for X and second for Y.