Home | Libraries | People | FAQ | More |
You may have an existing body of tests that you wish to register with Boost.Test
manually. You can use BOOST_TEST_CASE
to register a function taking no arguments and returning void
as a test case:
static void inserts_text() { std::ostringstream dest; hello_world(dest); BOOST_REQUIRE_EQUAL("Hello, world!\n", dest.str()); } static void register_function(test_suite& suite) { suite.add(BOOST_TEST_CASE(inserts_text)); }
You can use a test case method on a fixture class by registering a function that creates an instance of the fixture and invokes the test method. This assures that a fresh fixture is created for each test case, ensuring that each test executes independently of other tests.
class hello_fixture { public: void stream_with_badbit_throws_runtime_error() { dest.clear(std::ios_base::badbit); BOOST_REQUIRE_THROW(hello_world(dest), std::runtime_error); } std::ostringstream dest; };
static void stream_with_badbit_throws_runtime_error() { hello_fixture().stream_with_badbit_throws_runtime_error(); }; static void register_method_function_instance(test_suite& suite) { suite.add(BOOST_TEST_CASE(stream_with_badbit_throws_runtime_error)); }
You can register a method on a class as a test case by using Boost.Bind to bind the method to a static instance of the class. This can cause oen test case to influence the execution of another test case because they share the static fixture.
static hello_fixture hf; static void register_method_static_instance(test_suite& suite) { suite.add(BOOST_TEST_CASE( boost::bind(&hello_fixture::stream_with_badbit_throws_runtime_error, &hf))); }
A static instance of the class is used so that the instance exists at the time the test case is invoked. Test case invocation happens after registration, so using a local instance in the registration function is insufficient.
A new test suite is created with the BOOST_TEST_SUITE
macro. The new suite must be added to the master test suite to execute its
tests. Any number of test suites can be added in a hierarchy of suites and
any test case can be added to any test suite.
static test_suite* init_unit_test_suite(int argc, char* argv[]) { test_suite* hello_suite = BOOST_TEST_SUITE("hello"); register_function(*hello_suite); register_method_static_instance(*hello_suite); register_method_function_instance(*hello_suite); framework::master_test_suite().add(hello_suite); return 0; }