Engineerist
Engineerist

Reputation: 367

Boost Unit test framework fixtures using free functions

How does one use free functions for fixtures (setup and teardown) as hinted here: flexible models ? The docs don't show an example and the library tests do not use this scenario. I am looking for an example for a test suite.

Upvotes: 0

Views: 406

Answers (1)

Engineerist
Engineerist

Reputation: 367

FWIW, this works for me:

#define BOOST_TEST_MODULE foo

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

void setup ()    { BOOST_TEST_MESSAGE("set up fun"); }
void teardown () { BOOST_TEST_MESSAGE("tear down fun"); }

BOOST_AUTO_TEST_SUITE(bar, *utf::fixture (&setup, &teardown))

BOOST_AUTO_TEST_CASE(test1) {
    BOOST_TEST_MESSAGE("running test1");
    BOOST_TEST(true);
}

BOOST_AUTO_TEST_CASE(test2) {
    BOOST_TEST_MESSAGE("running test2");
    BOOST_TEST(true);
}

BOOST_AUTO_TEST_SUITE_END()

Running:

$ clang++ -I/usr/local/include test.cpp && ./a.out --log_level=all
Running 2 test cases...
Entering test module "foo"
test.cpp:9: Entering test suite "bar"
set up fun
test.cpp:11: Entering test case "test1"
running test1
test.cpp:13: info: check true has passed
test.cpp:11: Leaving test case "test1"; testing time: 56us
test.cpp:16: Entering test case "test2"
running test2
test.cpp:18: info: check true has passed
test.cpp:16: Leaving test case "test2"; testing time: 36us
tear down fun
test.cpp:9: Leaving test suite "bar"; testing time: 148us
Leaving test module "foo"; testing time: 244us
...

Notice the call to setup before the suite is run and the call to teardown at the end.

Upvotes: 1

Related Questions