|
Boost Users : |
Subject: Re: [Boost-users] Unit Tests and function calling
From: Richard (legalize+jeeves_at_[hidden])
Date: 2014-02-19 14:10:52
[Please do not mail me a copy of your followup]
boost-users_at_[hidden] spake the secret code
<CANO4BK-7KsF+siUdw6jr4JvQNSfrQ5hfWY7bO7e2TMZrXHDMSA_at_[hidden]> thusly:
>Just wanted a second opinion on how best to handle calling functions which
>invoke BOOST_REQUIRE and BOOST_CHECK type logic.
>
>For example, if I have a test like:
>
>void stuff( ... )
>{
> BOOST_REQUIRE( ... );
> BOOST_CHECK( ... );
>}
>
>BOOST_AUTO_TEST( check_stuff )
>{
> stuff( case1 );
> stuff( case2 );
> stuff( case3 );
>}
>
>in the CLF log case at least, I only see which lines in stuff failed, but
>not which which case.
>
>I can change things so that we have:
>
>bool stuff( ... )
>{
> bool result = true;
> if ( ( result = require-predicate ) == true )
> result = check-predicate;
> return result;
>}
>
>BOOST_AUTO_TEST( check_stuff )
>{
> BOOST_CHECK( stuff( case1 ) );
> BOOST_CHECK( stuff( case2 ) );
> BOOST_CHECK( stuff( case3 ) );
>}
>
>but then I lose the info in the logs over which predicates are failing and
>it's generally a bit nasty :).
Use the 2nd form of the code you have posted and have your custom
predicate return boost::test_tools::predicate_result instead of bool.
Then you can build as an elaborate failure message as you need in
order to understand the cause of a failing test case.
You get the benefits of a custom predicate, proper reporting of the
failure location in the test case, and a useful diagnostic message
when it fails.
See these pages:
<http://user.xmission.com/~legalize/tmp/boost.test/libs/test/doc/html/test/reference/test_classes/predicate_result.html>
<http://user.xmission.com/~legalize/tmp/boost.test/libs/test/doc/html/test/reference/assertion/boost_level_message.html>
You have been able to do this for a long time, but it was never
properly documented and I don't know why the maintainer of Boost.Test
never mentions it when these questions about custom assertions come
up.
-- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net