How can I check if there have been any BOOST_CHECK tests so far?

I have a test case that does some validation with BOOST_CHECK*

so the failures don't immediately stop the test. But at some point I would like to stop if there have been any test failures so far, because the rest of the test is pointless to run if the health check fails? 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());
}

      

+3


source to share


2 answers


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

      

+4


source


BOOST_CHECK

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



BOOST_REQUIRE

, on the other hand, asserts the condition required for the test to continue. Use this macro for assertions that should abort the test if it fails. In your case, it looks like you want to use this for every statement before foo.do_something()

.

+1


source







All Articles