Home > The Unit Test Framework > User's guide > Test organization > Nullary function based test case > Manual registration
PrevNext

Manually registered nullary function based test case

To create a test case manually, employ the macro BOOST_TEST_CASE:

BOOST_TEST_CASE(test_function)

BOOST_TEST_CASE creates an instance of the class boost::unit_test::test_case and returns a pointer to the constructed instance. The test case name is deduced from the macro argument test_function. If you prefer to assign a different test case name, you have to use the underlying make_test_case interface instead. To register a new test case, employ the method test_suite::add. Both test case creation and registration are performed in the test module initialization function.

Here is the simplest possible manually registered test case. This example employs the original test module initialization function specification. A single test case is created and registered in the master test suite. Note that the free function name is passed by address to the macro BOOST_TEST_CASE.

Example 5. Nullary free function manually registered

#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

void free_test_function()
{
    BOOST_CHECK( true /* test assertion */ );
}

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( &free_test_function ) );

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...

*** No errors detected

A test case can be implemented as a method of a class. In this case a pointer to the class instance has to be bound to the test method to create a test case. You can use the same instance of the class for multiple test cases. The UTF doesn't take an ownership of the class instance and you are required to manage the class instance lifetime yourself.

[Warning] Warning

The class instance can't be defined in the initialization function scope, since it becomes invalid as soon as the test execution exits it. It needs to be either defined statically/globally or managed using a shared pointer.

Example 6. Nullary method of a class bound to global class instance and manually registered

#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

class test_class {
public:
    void test_method()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
} tester;

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method, &tester )));

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(11): error in "boost::bind( &test_class::test_method, &tester )": check false failed

*** 1 failure detected in test suite "Master Test Suite"

Example 7. Nullary method of a class bound to shared class instance and manually registered

#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

class test_class {
public:
    void test_method1()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
    void test_method2()
    {
        BOOST_CHECK( false /* test assertion */ );
    }
};

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    boost::shared_ptr<test_class> tester( new test_class );

    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method1, tester )));
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method2, tester )));
    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 2 test cases...
test.cpp(15): error in "boost::bind( &test_class::test_method2, tester )": check false failed

*** 1 failure detected in test suite "Master Test Suite"

If you do not need to reuse the test class instance and can't or do not wish to create test class instance globally it may be easier and safer to create an instance on the stack of free function:

Example 8. Nullary method of a class bound to local class instance inside free function

#include<boost/test/included/unit_test.hpp>

using namespace boost::unit_test; 

class test_class {
public:
    void test_method()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
};

//____________________________________________________________________________//

void free_test_function() 
{ 
    test_class inst; 

    inst.test_method(); 
} 

//____________________________________________________________________________//

test_suite* 
init_unit_test_suite( int argc, char* argv[] ) 
{ 
    framework::master_test_suite().add( BOOST_TEST_CASE( &free_test_function ) ); 

    return 0; 
} 

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...

*** No errors detected

If you have to perform the same set of tests with different sets of parameters you may want to base your test case on a function with arguments and bind particular parameters during test case creation.

[Warning] Warning

If you bind parameters by reference or pointer, the referenced value can't have local storage in the test module initialization function.

Example 9. Binary free function bound to set of different parameter pairs

This example employs the alternative test module initialization function specification.

#define BOOST_TEST_DYN_LINK
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

void free_test_function( int i, int j )
{
    BOOST_CHECK( true /* test assertion */ );
}

//____________________________________________________________________________//

bool
init_function()
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 1, 1 ) ) );
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 1, 2 ) ) );
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 2, 1 ) ) );

    return true;
}

//____________________________________________________________________________//

int
main( int argc, char* argv[] )
{
    return ::boost::unit_test::unit_test_main( &init_function, argc, argv );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 3 test cases...

*** No errors detected

The UTF also presents an alternative method for parameterized test case creation, which is covered in the section called “Unary function based test case”.


PrevUpHomeNext