【问题标题】:Detect if boost test case failed检测 boost 测试用例是否失败
【发布时间】:2013-08-30 06:49:39
【问题描述】:

我想记录更多关于 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 对象中的条件是什么。理想情况下,我希望它们被转储一次,即使测试用例中发生了多个断言。

【问题讨论】:

  • 完全尝试在项目中做同样的事情。
  • 我没有找到任何解决方案...

标签: c++ unit-testing boost


【解决方案1】:

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

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_REQUIRE_...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2018-03-06
    • 2014-08-16
    • 2016-08-08
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多