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.

有帮助吗?

解决方案

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.

其他提示

You should look at Test case template as they provide a way to run a same set of tests using different data types. From what I understand, using the auto-registration macros, you can provide a test case template:

BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, test_types)
{
    BOOST_CHECK(checkSomethingWithType(T));
}

where tests_types is an MPL list of types:

typedef boost::mpl::list<X, Y> test_types;

Those example are extracted and adapted from the Boost test documentation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top