Boost.Test - how to print the value of a variable in case of error?

Hi, I am testing "stuff" in a loop, and the "failure message" from Boost.Test is not too useful, because it does not tell me the "loop variable" value. I have hit this "problem" several times already, not only in loops, but any times a variable is involved in a tested expression. And I was not able to figure out what to do. I've looked at the sources etc., but I am too stupid to see if there is a solution in there. I haven't seen anything in the documentation. :( Imagine this: BOOST_CHECK_EQUAL(cont.erase(key),1u); If it fails, it tells me this: basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] I cannot see the value of the 'key' variable, so it quite hard to know what that error is. In case of simple code, it may not be a problem to track down what key is, but when it is a loop of over 100000 iterations... :( ..._CHECKPOINT does not work, because it is only printed in case of an exception, but not other failures. :( I was hoping to find something like: BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) ); which would say: basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] with key == 12 Is there a way I could do that? I have tried to ask in #BOOST on IRC, but it seems there are only bots there. :( TIA, Attila

AMDG Attila Feher F wrote:
I am testing "stuff" in a loop, and the "failure message" from Boost.Test is not too useful, because it does not tell me the "loop variable" value. I have hit this "problem" several times already, not only in loops, but any times a variable is involved in a tested expression. And I was not able to figure out what to do. I've looked at the sources etc., but I am too stupid to see if there is a solution in there. I haven't seen anything in the documentation. :(
Imagine this:
BOOST_CHECK_EQUAL(cont.erase(key),1u);
If it fails, it tells me this:
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
I cannot see the value of the 'key' variable, so it quite hard to know what that error is. In case of simple code, it may not be a problem to track down what key is, but when it is a loop of over 100000 iterations... :(
..._CHECKPOINT does not work, because it is only printed in case of an exception, but not other failures. :(
I was hoping to find something like:
BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) );
which would say:
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] with key == 12
Is there a way I could do that? I have tried to ask in #BOOST on IRC, but it seems there are only bots there. :(
Have you looked at http://www.boost.org/libs/test/doc/html/utf/testing-tools/custom-predicate.h... In Christ, Steven Watanabe

Steven Watanabe wrote: [SNIP]
..._CHECKPOINT does not work, because it is only printed in case of an exception, but not other failures. :(
I was hoping to find something like:
BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) );
which would say:
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] with key == 12
Is there a way I could do that? I have tried to ask in #BOOST on IRC, but it seems there are only bots there. :(
Have you looked at http://www.boost.org/libs/test/doc/html/utf/testing-tools/custom-predicate.h...
I have, but I am looking for a simple solution that would not change the look of the test code too much. That is something I wish to avoid. Especially if it means changing the test asserts to something that needs to be documented and taught to all (150+ people). Attila

[Please do not mail me a copy of your followup]
boost-users@lists.boost.org spake the secret code
Imagine this:
BOOST_CHECK_EQUAL(cont.erase(key),1u);
First, I recommend use of BOOST_REQUIRES_xxx instead of BOOST_CHECK_xxx.
If it fails, it tells me this:
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
If you use BOOST_CHECK_MESSAGE(predicate, message), you can create a
custom failure message that contains additional context:
#define BOOST_TEST_MODULE example
#include

Richard wrote: [SNIP]
If it fails, it tells me this:
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
If you use BOOST_CHECK_MESSAGE(predicate, message), you can create a custom failure message that contains additional context:
#define BOOST_TEST_MODULE example #include
#include <cmath>
BOOST_AUTO_TEST_CASE( test ) { double res = std::sin( 45. );
BOOST_WARN_MESSAGE( res > 1, "sin(45){" << res << "} is <= 1. Hmm.. Strange. " ); }
Thanks! I was doing that earlier, but I was hoping there is a "better" solution. With this, I have to do all the things "by hand", so for example the spelling out of the two sides of an EQUAL check. :( It seems that I have to "duplicate" all existing macros and create something like a BOOST_CHECK_..._WITH(...) series. Eg.: BOOST_CHECK_EQUAL_WITH(lhs,rhs,v1,v2) used as: BOOST_CHECK_EQUAL_WITH(a[i],b[j],i,j) output: tests.cpp(99): error in "test_case": check a[i] == b[j] [with i==1, j==42] failed [0 != 1] BR, Attila aka WW

[Please do not mail me a copy of your followup]
boost-users@lists.boost.org spake the secret code
Richard wrote:
BOOST_WARN_MESSAGE( res > 1, "sin(45){" << res << "} is <= 1. Hmm.. Strange. " ); }
Thanks! I was doing that earlier, but I was hoping there is a "better" solution.
Yeah, most assertion frameworks have some way to optionally provide a message. Since Boost.Test's assertions are done with macros, there isn't a way to provide an overloaded version that supplies a contextual message. I'm afraid the best you can do is use BOOST_REQUIRE_MESSAGE() or provide your own assertion macros on top of it. -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/ Legalize Adulthood! http://legalizeadulthood.wordpress.com
participants (3)
-
Attila Feher F
-
legalize+jeeves@mail.xmission.com
-
Steven Watanabe