The class test case

Boost.Test > Components > The Unit Test Framework > Components > The Test Case > The class member function based test case

# The class member function based test cases

##### Definition

defined in unit_test_suite.hpp

##### Synopsis

    template<class UserTestClass>
    class class_test_case : public test_case
    {
    public:
        class_test_case( void (UserTestCase::*function_type)(),
                         std::string const& name,
                         boost::shared_ptr<UserTestCase>& user_test_case );

        ... // Implementation
    };

##### Description

Instances of the class class_test_case are created by the framework for the supplied member function and instance of the user's test class with the following specification: void (UserTestClass::*fct)(). To allow sharing on the same instance, so that one test case could use results of another, class_test_case is constructed based on boost::shared_ptr to the user test case class instance. Note, though, that we could achieve similar effect with use of free function like this:

    void compound_test() {
        UserTestCase instance;

        instance.test1();
        instance.test2();
        ...
    };

The only drawback of this version that you could not separate results of one test case from another. Another reason to use class member function based test cases is to test non default constructible user test case. In other words, if user test case need command line arguments or some other parameters to be constructed. As a general rule it is recommended to use the class_test_case only if you can't implement a test logic in a free function. Due to usage of templates in an implementation of the class_test_case, a compilation can be longer would you use the function_tc.

##### Construction

To create an instance of the class class_test_case use the following macro:

BOOST_CLASS_TEST_CASE( member_function_address, shared_tc_instance ).

BOOST_CLASS_TEST_CASE creates a new instance of the class class_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 of above macro is the pointer to the class member function - body of the test case. The second parameter is the shared instance of class that should be used when invoking this test case.

##### Examples

    class my_complex_test {
    public:
        void test_feature1() {
            ...
        }
    };
    ...

    boost::shared_ptr<my_complex_test> instance( new my_complex_test );
    ts->add( BOOST_CLASS_TEST_CASE( &my_complex_test::test_feature1, instance ) );

______________________________________________________

    class class_under_test {
    public:
        // i should be positive; throw an exception otherwise
        explicit class_under_test( int i );
        ...
        // access methods
        int get_value() const;
    };

    class compound_test {
    public:
        void test_construction() {
            BOOST_CHECK_THROW( new class_under_test( -1 ) );

            v = new class_under_test( 1 );

            BOOST_CHECK( v is valid );
            ...
        }

        void test_access_methods() {
            BOOST_CHECK_EQUAL( v->get_value(), 1 );
            ...
        }
    private:
        class_under_test* v;
    };
    ...

    boost::shared_ptr<compound_test> instance( new compound_test );
    Ts>add( BOOST_CLASS_TEST_CASE( &compound_test::constructor, instance ) );
    Ts>add( BOOST_CLASS_TEST_CASE( &compound_test::test_access_methods, instance ) );