检测升压测试用例是否失败

Detect if boost test case failed

本文关键字:是否 失败 测试用例 检测      更新时间:2023-10-16

我想记录更多关于BOOST断言失败的数据。不确定这是否可能以及如何实现。

BOOST_AUTO_TEST_CASE( TestCase1 )
{
    Data d;
    d.fillUp(...);
    d.operation1(...);
    BOOST_CHECK(d == ...);
    d.operation2(...);
    BOOST_CHECK(d == ...);
    ...
    if( /* anything above failed */)
    {
        log << d;
    }
}

我对最后一种情况有意见。你能给我建议吗?我希望错误日志能够指示断言发生时Data对象中的条件。理想情况下,我希望它们被丢弃一次,即使测试用例中发生了多个断言。

我正在做以下事情来完成您想要的:

BOOST_CHECK_MESSAGE( current_test_passing(), d);

使用我刚刚添加到测试助手函数集合中的以下函数:

#include <boost/test/results_collector.hpp>
inline bool current_test_passing()
{
  using namespace boost::unit_test;
  test_case::id_t id = framework::current_test_case().p_id;
  test_results rez = results_collector.results(id);
  return rez.passed();
}

我发现这对与BOOST_REQIRE_结合的循环很有用……所以我可以快速查看许多检查中的任何一个失败的特定迭代,而不必在每个检查中添加"I="消息:

for (int i=0; i < HUGE_NUMBER; ++i) {
  … many checks …
  BOOST_REQUIRE_MESSAGE( current_test_passing(), "FAILED i=" << i );
}