BOOST_CHECK_EQUAL (and dervatives) Add custom post

We recently started using the Boost Test framework and as before. However, there are certain tests where it would be great if we could add custom messages to an existing helper.

For example, I can get the output in mytest and mytest2, but haven't found a way to get the result in mytest3:

#define BOOST_TEST_MODULE mytests
#include <boost/test/unit_test.hpp>


BOOST_AUTO_TEST_SUITE(myunit)

BOOST_AUTO_TEST_CASE(mytest)
{
    // This give a nice output [2+2 != 5]
    BOOST_CHECK_EQUAL(2+2, 5);
}

BOOST_AUTO_TEST_CASE(mytest2)
{
    // This give only a custom output
    BOOST_CHECK_MESSAGE(2+2 == 5, "comparison error");
}

BOOST_AUTO_TEST_CASE(mytest3)
{
    // Ideally, it should output [2+2 != 5] comparison error
    BOOST_CHECK_EQUAL_WITH_ADDITIONAL_MESSAGE(2+2, 5, "comparison error");
}

BOOST_AUTO_TEST_SUITE_END()

      

The reason I want this is because if I want to have test cases like this:

BOOST_AUTO_TEST_CASE(mytest4)
{
    for(int i = 0; i < 10; ++i)
    {
        BOOST_CHECK_EQUAL_WITH_ADDITIONAL_MESSAGE(i%3, 0, i);
    }
}

      

In this case, there is no way to know why I failed the test.

I tried to "duplicate" the BOOST_CHECK_EQUAL macro, as shown in increasing hopes, will be appended to the passed message when the original macro passes an empty literal:

#define BOOST_CHECK_EQUAL2( L, R ) \
    BOOST_CHECK_WITH_ARGS_IMPL( ::boost::test_tools::tt_detail::equal_impl_frwd(), "hello world", CHECK, CHECK_EQUAL, (L)(R) )

      

However, "hello world:" is overwritten somewhere in the test implementation with an unfortunate condition.

Is there a (simple and clean) way to solve this problem?

UPDATE It looks like the implementation of check_impl () in test_tools.ip does not use the check_descr parameter for equality checks.

Is there an elegant way to override / provide my own?

+3


source to share


2 answers


Ok, I just would like to post for reference, in case anyone else runs into this, I solved it this way:

//____________________________________________________________________________//

#define BOOST_TEST_REL_EQ_MESSAGE_EXTENSION(L, R, M, CMP, ICMP, CT)         \
    {                                                                       \
        auto _1(L);                                                         \
        auto _2(R);                                                         \
        std::stringstream ss;                                               \
        ss << "check " << BOOST_TEST_STRINGIZE(L) << " " << BOOST_TEST_STRINGIZE(CMP) << " " << BOOST_TEST_STRINGIZE(R) << " failed [" << _1 << " " << BOOST_TEST_STRINGIZE(ICMP) << " " << _2 << "] : " << M;\
        BOOST_CHECK_IMPL( (_1 CMP _2), ss.str(), CT, CHECK_MSG );           \
    }                                                                       \
/**/

#define BOOST_CHECK_EQUAL_MESSAGE(L, R, M)      BOOST_TEST_REL_EQ_MESSAGE_EXTENSION(L, R, M, ==, !=, CHECK )
#define BOOST_WARN_EQUAL_MESSAGE(L, R, M)       BOOST_TEST_REL_EQ_MESSAGE_EXTENSION(L, R, M, ==, !=, WARN )
#define BOOST_REQUIRE_EQUAL_MESSAGE(L, R, M)    BOOST_TEST_REL_EQ_MESSAGE_EXTENSION(L, R, M, ==, !=, REQUIRE )

      

While this may not be optimal (mainly due to the fact that stringstream is used in every iteration in mytest4 above), it looks like it provides a reasonably clean and non-intrusive solution for a few cases where additional message might be needed



UPDATE 2017-08

For newer versions of test testing, we can use BOOST_TEST_INFO () to output a message, which is much cleaner:

#define BOOST_CHECK_EQUAL_MESSAGE(L, R, M)      { BOOST_TEST_INFO(M); BOOST_CHECK_EQUAL(L, R); }
#define BOOST_WARN_EQUAL_MESSAGE(L, R, M)       { BOOST_TEST_INFO(M); BOOST_WARN_EQUAL(L, R); }
#define BOOST_REQUIRE_EQUAL_MESSAGE(L, R, M)    { BOOST_TEST_INFO(M); BOOST_REQUIRE_EQUAL(L, R); }

      

+2


source


For the need you describe, you must use the concept of context .



Otherwise, assertion BOOST_TEST

(and here too ) supports a string as a second argument that can be used to display a custom message.

0


source







All Articles