How can I tell if any BOOST_CHECK tests have failed so far?

2k views Asked by At

I have a boost test case that does some checks using BOOST_CHECK*, so failures don't immediately stop the test. But at some point, I'd like to stop if any test failures have occurred so far, because the rest of the test is pointless to run if the sanity checks have failed? For example:

BOOST_AUTO_TEST_CASE(test_something) {
    Foo foo;
    BOOST_CHECK(foo.is_initialized());
    BOOST_CHECK(foo.is_ready());
    BOOST_CHECK(foo.is_connected());
    // ...

    // I want something like this:
    BOOST_REQUIRE_CHECKS_HAVE_PASSED();

    foo.do_something();
    BOOST_CHECK(foo.is_successful());
}
2

There are 2 answers

0
Tavian Barnes On BEST ANSWER

The state of the current test can be checked as follows:

namespace ut = boost::unit_test;
auto test_id = ut::framework::current_test_case().p_id;
BOOST_REQUIRE(ut::results_collector.results(test_id).passed());
3
cdhowie On

BOOST_CHECK asserts on a condition that is required for the test to pass, but is not required for the test to continue executing.

BOOST_REQUIRE on the other hand asserts on a condition that is required for the test to continue. Use this macro for asserts that should abort the test on failure. In your case it looks like you want to use this for every assert prior to foo.do_something().