views:

202

answers:

2

I would like to write a method in my test suite as follows:

void checkParams(arg1, arg2, arg3)
{
BOOST_REQUIRE(arg1==1);
BOOST_REQUIRE(arg2==2);
BOOST_REQUIRE(arg3==3);

}

However, I want to write something to stderr if the assert fails. For example, if the first assert fails, I want to write: checkParams failed with arguments arg1=5, arg2=4, arg3=3

Write now the message it generates is just that critical check failed 5==1.

More generally, I would like to create a stacktrace on failure so that I can see a chain of calls that led to this failure. Note that this method is called by all my BOOST_AUTO_TEST_CASE methods but is not itself a BOOST_AUTO_TEST_CASE.

How do I do this?

Another question: I know I can modify the logging level by providing run time parameters,

./test --log_level=all

How do I set the log_level within code?

+2  A: 

You want BOOST_REQUIRE_MESSAGE.

You also probably want to merge those into a single check:

BOOST_REQUIRE_MESSAGE((arg1 == 1) && (arg2 == 2) && (arg3 = 3),
   "failed with arg1=" << arg1 << " arg2=" << arg2 << " arg3= " << arg3);
R Samuel Klatchko
Boost 1.40 does not seem to allow me to place a BOOST_MESSAGEas the second parameter (only a literal string).
Oops, put in BOOST_MESSAGE when that wasn't needed. Updated the answer with the correct version.
R Samuel Klatchko
+1  A: 

I would use BOOST_CHECK or BOOST_CHECK_EQUAL before the BOOST_REQUIRE. BOOST_CHECK just reports the error and continues, so the test fails, but you get to see all the wrong values.

If you want to force the test to stop afterwards use BOOST_REQUIRE afterwards.

void checkParams(arg1, arg2, arg3)
{
    BOOST_CHECK_EQUAL(1, arg1);
    BOOST_CHECK_EQUAL(2, arg2);
    BOOST_CHECK_EQUAL(3, arg3);

    BOOST_REQUIRE(arg1==1 && arg2==2 && arg3==3);
}
iain