开发者

C++ boost unit test error message

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 开发者_StackOverflow社区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?


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);


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜