Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

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

Writer Mia Lopez

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 Answers

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

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.