Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if boost test case failed

I'd like to log some more data on BOOST's assertion failure. Not sure if this is possible and how.

BOOST_AUTO_TEST_CASE( TestCase1 )
{
    Data d;

    d.fillUp(...);

    d.operation1(...);
    BOOST_CHECK(d == ...);

    d.operation2(...);
    BOOST_CHECK(d == ...);

    ...

    if( /* anything above failed */)
    {
        log << d;
    }
}

I am having problem with the last condition. Can you advise? I'd like the error log to indicate what were the conditions in the Data object when assertions happened. Ideally i would like them to be dumped once, even if multiple assertions in the testcase happened.

like image 799
Patrik Beck Avatar asked Aug 27 '13 13:08

Patrik Beck


1 Answers

I'm am doing the following to accomplish what you want:

BOOST_CHECK_MESSAGE( current_test_passing(), d);

using the following function that I have just added to my collection of test helper functions:

#include <boost/test/results_collector.hpp>

inline bool current_test_passing()
{
  using namespace boost::unit_test;
  test_case::id_t id = framework::current_test_case().p_id;
  test_results rez = results_collector.results(id);
  return rez.passed();
}

I'm finding is useful for loops in combination with BOOST_REQUIRE_… so I can quickly see for which particular iteration any of many checks are failing without having to add the "i=" message to every check:

for (int i=0; i < HUGE_NUMBER; ++i) {
  … many checks …
  BOOST_REQUIRE_MESSAGE( current_test_passing(), "FAILED i=" << i );
}
like image 126
Castedo Avatar answered Oct 28 '22 17:10

Castedo