Boost.Test > Components > The Unit Test Framework > Components > The Test Case > The parameterized boost::function based test case
Boost Test logo

The parameterized boost::function based test case

Definition

defined in unit_test_suite_ex.hpp

Synopsis
template<typename ParamIterator, typename ParameterType>
class parameterized_boost_function_test_case : public test_case
{
    ... // Implementation
};
Description

In some cases it may be convenient to be able to use complex arity one functor as a body of parameterized test case. It could be free function with bound argument, compound functor or member function with bound object. All these and similar cases does not covered by simple basic test case classes. This is the need addressed by class template parameterized_boost_function_test_case. It uses boost::function1<void,ParameterType> as a test function type. This facility is more generic and more powerful then simpler basic test case classes supplied in the header unit_test_suite.hpp . It comes for the price of extra dependency on boost::function facility. Accordingly it does not gets included automatically by default, but instead supplied as an extension for the framework. You need to include header unit_test_suite_ex.hpp yourself to be able to use it.

This facility is flexible enough to accommodate most needs of testing with parameters, including free function test cases, class member function based test cases, functions with bound parameters and so on.

Construction

To create a test case based on any zero arity function or function object use the following macro:

BOOST_PARAM_TEST_CASE( arity_one_function, parameters_begin, parameters_end).

BOOST_PARAM_TEST_CASE creates a new instance of the class template parameterized_boost_function_test_case and returns a pointer to the base class test_case. In most cases you will use it as an argument to the method test_suite::add(...). The first parameter to above macro is the pointer or reference to arity one function or function object - body of the test case. The parameters_begin and parameters_end are begin and end of parameters sequence. Be aware that the parameterized_boost_function_test_case does not store list of parameters internally. The user should make sure that parameters list will not be destroyed until the test case is run. That's why it not recommended to create a parameters list as local variable in init_unit_test_suite. A simple way to handle a parameters list lifetime is to place it into a user defined test suite class.

Example
#include <boost/test/unit_test_ex.hpp>
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
#include <boost/bind.hpp>

#include <list>

void test_mask( int arg, int mask )
{
    BOOST_MESSAGE( arg << " " << mask );


    BOOST_CHECK( (arg & mask) != 0 );
}

struct sub_test_suite : public test_suite {
    sub_test_suite() 
    {
	        parameters_list.push_back( 1 );
	        parameters_list.push_back( 5 );
	        parameters_list.push_back( 6 );
	        parameters_list.push_back( 7 );
	        parameters_list.push_back( 140 );


        add( BOOST_PARAM_TEST_CASE( boost::function1<void,int>( bind( &test_mask, _1, 0x80 ) ),
                                    parameters_list.begin(), parameters_list.end() ) );
    }

    std::list<int> parameters_list;
	}
//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) {
    test_suite* test = BOOST_TEST_SUITE( "Example" );

    test->add( new sub_test_suite );

    return test;
}