Search code examples
c++unit-testingboost

Is it possible to use BOOST_PARAM_TEST_CASE with automatic registration on boost::test?


Is it possible to mix up the BOOST_AUTO_TEST_CASE and BOOST_AUTO_TEST_CASE_TEMPLATE macros with the BOOST_PARAM_TEST_CASE in any way? I'm even interested in really messy ways of making this happen.

Having to build all of your test cases by hand seems really tedious. But the BOOST_PARAM_TEST_CASE mechanism is pretty darn useful, but only works if you have a test init function, which in turn requires you to have be using manual test case construction.

Is there any documentation on how to hook into the automated system yourself so you can provide your own tests that auto-register themselves?

I'm using boost 1.46 right now.


Solution

  • Starting with Boost version 1.59, this is being handled by data-driven test cases:

    #define BOOST_TEST_MODULE MainTest
    
    #include <boost/test/included/unit_test.hpp>
    #include <boost/test/data/test_case.hpp>
    #include <boost/array.hpp>
    
    static const boost::array< int, 4 > DATA{ 1, 3, 4, 5 };
    
    BOOST_DATA_TEST_CASE( Foo, DATA )
    {
        BOOST_TEST( sample % 2 );
    }
    

    This functionality requires C++11 support from compiler and library, and does not work inside a BOOST_AUTO_TEST_SUITE.

    If you have to support both old and new versions of Boost in your source, and / or pre-C++11 compilers, check out And-y's answer.