Is there a way to ensure lazy evaluation of BOOST_TESTs message?

  boost-test, c++

Is it possible to ensure that the message macro parameter of BOOST_TEST is only evaluated, if the check actually fails. Lazy evaluation seems to happen for human readable output format, but not for JUnit output format. Can I somehow make lazy evaluation work reliably for all output types?


#define BOOST_TEST_MODULE my_module

#include <boost/test/included/unit_test.hpp>
#include <string>

struct S
    std::string m_value;

S* f(void)
    return nullptr;

    S* result = f();
    BOOST_TEST(result == nullptr, "f() should return nullptr but returned a object with m_value = " << result->m_value);

This code works fine if I use human readable output format (--log_format=HRF command line option for executable). Using JUnit output (--log_format=JUNIT command line option) results in a access error, since the program attempts to get the size of a string at address 0.

This is a bit unexpected, since I’d assume the BOOST_TEST macro would work similar to this

// not the real macro, but one that works in a way I'd expect it to work
#define BOOST_TEST(condition, message) if (!(condition)) { 
    some_output_stream << "The following test failed: " << #condition << std::endl 
                       << "Message: " << message; 

Tested configurations:

  • boost 1.75.0
  • Visual Studio 2017/2019 or g++ 8.x (Win 10 / Ubuntu 18)
  • 64 bit

Source: Windows Questions C++