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

The boost::function based test case

Definition

defined in unit_test_suite_ex.hpp

Synopsis
class boost_function_test_case : public test_case
{
    ... // Implementation
};
Description

In some cases it may be convenient to be able to use complex zero arity functor as a body of 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 boost_function_tc. It uses boost::function0<void> as a test function type. This facility is more generic and more powerful then more simple 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 parameterless testing, 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_TEST_CASE( zero_arity_function ).

BOOST_TEST_CASE creates a new instance of the class 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 only parameter to above macro is the pointer or reference to zero arity function or function object - body of the test case.

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

void test_mask( int arg, int mask )
{
    BOOST_CHECK( (arg & mask) != 0 );
}

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

    test->add( BOOST_TEST_CASE( bind( &test_mask, 12345, 0xcdf ) ) );

    return test;
}