Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

BOOST_PARAM_CLASS_TEST_CASE

BOOST_PARAM_CLASS_TEST_CASE(memfn, instance, begin, end) works similarly to BOOST_PARAM_TEST_CASE. It creates a collection of test cases that invoke the member function memfn on the instance pointer instance. The member function is passed an item obtained by iterating the collection defined by the begin and end iterators. The instance pointer must be passed as a boost::shared_ptr to ensure the lifetime of the instance exceeds that of any test case using the instance. The test case returned by BOOST_PARAM_CLASS_TEST_CASE must be added to a test suite in an initialization function for the tests to be executed.

#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::unit_test;

class hello_world_tester
{
public:
    hello_world_tester()
    {
        eq_items.push_back("Hello, world!\n");
        eq_items.push_back("Hello, world!\n");
        ne_items.push_back("shaggy");
        ne_items.push_back("scooby");
    }

    void inserts_text(std::string const& text)
    {
        std::ostringstream dest;

        hello_world(dest);

        BOOST_REQUIRE_EQUAL(text, dest.str());
    };

    void does_not_insert_text(std::string const& text)
    {
        std::ostringstream dest;

        hello_world(dest);

        BOOST_REQUIRE_NE(text, dest.str());
    }

    std::vector<std::string> eq_items;
    std::vector<std::string> ne_items;
};

boost::shared_ptr<hello_world_tester> tester(boost::make_shared<hello_world_tester>());

static test_suite*
init(int argc, char* argv[])
{
    test_suite& suite = framework::master_test_suite();
    suite.add(BOOST_PARAM_CLASS_TEST_CASE(
        &hello_world_tester::inserts_text, tester,
        tester->eq_items.begin(), tester->eq_items.end()));
    suite.add(BOOST_PARAM_CLASS_TEST_CASE(
        &hello_world_tester::does_not_insert_text, tester,
        tester->ne_items.begin(), tester->ne_items.end()));
    return 0;
}

int main(int argc, char* argv[])
{
    return unit_test_main(init, argc, argv);
}

[Note] Note

The single instance is shared among all test cases and therefore should not keep any shared state that may couple independent test cases. In this example, the output string stream is created fresh inside each test method and not shared between calls to the method.

Example Source Code

PrevUpHomeNext