简体   繁体   中英

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

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());
}

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());

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM