Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2006-07-26 12:03:32


"Stephan Tolksdorf" <andorxor_at_[hidden]> wrote in message
news:44C66F85.5020505_at_gmx.de...
> Gennadiy,
>
> while we are discussing exceptions in Boost::Test, I've got a small
> request:
>
> Could you maybe provide an equivalent macro to BOOST_CHECKPOINT (or
> extend BOOST_CHECKPOINT) whose message is also displayed if
> BOOST_REQUIRE fails? The reason I ask is that I sometimes outsource
> common testing routines into helper functions and would like to print
> context information if the test fails (as the line number itself is not
> very informative in these instances). I'm sure that would be a help to
> others too.

BOOST_CHECKPOINT would help you in this case. Lets consider your example:

void testHelper(int param1, int param2) {
   std::ostringstream msg;
   msg << "Problem with param1: " << param1 << " Param2: " << param2 << "...
\n";
   BOOST_CHECKPOINT(msg.str());

   // do some complicated operations
   (...)
   BOOST_REQUIRE_EQUAL(some_resulta, some_resultb);
   (...)
   // some more complicated operation
   BOOST_REQUIRE_EQUAL(some_resultc, some_resultd);
}

BOOST_AUTO_TEST_CASE(MyTest) {
 testHelper( 1, 3);
 testHelper( 7, 7);
 for (int i = 13; i < 51) {
  testHelper( 13, i);
 }
}

1. Checkpoint in terms of Boost.Test is named passpoint. And passpoint is
the position in a code test pass through. Primary purpose of passpoint is to
report last position passed in case of fatal error or uncaught exception.
Both BOOST_REQUIRE_EQUAL statements above will overwrite the checkpoint
position. Even if BOOST_REQUIRE would report checkpoint position, it would
not help you to separate 2 calls to testHelper.

2. The same issue actually applies to any Boost.Test tool: if tool is used
in shared location there is no way to know the context: where the shared
code was called from. Since in C++ there is no portable automatic traceback
facility, we need some kind of explicit scope tracking.

One solution is to use BOOST_TEST_MESSAGE based solution:

struct boost_test_scope {
  boost_test_scope( const_string scope_name ) // report "Enter scope
scope_name"
  ~boost_test_scope() // report "Leaving scope scope_name"
};

#define BOOST_TEST_SCOPE( SC ) \
    boost_test_scope scope_guard # __LINE__(
(boost::wrap_stringstream().ref() << M).str() )
/**/

There 2 issues with this solution:
1. You need to setup log_level=message
2. Messages are reported always whether or not there were errors in this
scope.

An alternative would be new facility: test context. Boost.Test will keep
track of registered contexts and will report the context with any failed
assertion. We could also have a parameter that turn on/off context
reporting.

If this is acceptable solution I could implement this in 1.35.

Gennadiy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk