Boost Test: how to write parameterized test cases

I have a test test. Most of the lines in this test case run independently of the parameters. But there are parts that are executed based on the provided parameter. I want to avoid writing two separate test cases that are almost identical, with the exception of some minor parts. Therefore, to create parameterized test cases, I need to use something like the following approach:

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 the BOOST_PARAM_CLASS_TEST_CASE macro, but I'm not sure if this matters in this case.

+7
source share
1 answer

There is no Boost AFAIK support, 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 this with templates if you like:

 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>(); } 
+1
source

All Articles