首页 > 解决方案 > 有没有办法确保对 BOOST_TESTs 消息进行惰性评估?

问题描述

如果检查实际上失败,是否可以确保BOOST_TEST仅评估消息宏参数。延迟评估似乎发生在人类可读的输出格式上,但不适用于 JUnit 输出格式。我可以以某种方式使惰性评估对所有输出类型都可靠地工作吗?

MCVE

#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;
}

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

如果我使用人类可读的输出格式(--log_format=HRF可执行文件的命令行选项),此代码可以正常工作。使用 JUnit 输出(--log_format=JUNIT命令行选项)会导致访问错误,因为程序会尝试获取 address 处字符串的大小0

这有点出乎意料,因为我假设BOOST_TEST宏的工作方式与此类似

// 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; \
}

测试配置:

标签: c++boost-test

解决方案


推荐阅读