3

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());
}
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118

2 Answers2

6

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());
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
1

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

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • That works, but I'm using `BOOST_CHECK` intentionally so that I can see if more than one thing is wrong at the beginning. I'd really like to keep that. – Tavian Barnes Nov 18 '14 at 20:10
  • @TavianBarnes I looked around in [the source](http://www.boost.org/doc/libs/1_35_0/boost/test/test_tools.hpp) and it doesn't seem to store any state that you could use to accomplish this. However, you may be able to write your own wrapper macro to accomplish this. I might see if I can come up with one and add it to my answer. – cdhowie Nov 19 '14 at 00:07
  • Looks like [this](https://github.com/boostorg/test/blob/master/include/boost/test/results_collector.hpp#L66) will tell me what I need to know – Tavian Barnes Nov 19 '14 at 16:11