【问题标题】:Does BOOST_DATA_TEST_CASE always require printability of the samples?BOOST_DATA_TEST_CASE 是否总是需要样品的可打印性?
【发布时间】:2020-06-05 08:18:49
【问题描述】:

我正在尝试使用 BOOST_DATA_TEST_CASE 运行一些数据驱动的测试用例,并弄清了迄今为止的基础知识。

但是,我注意到用作示例输入的类型必须是可打印的:

这将起作用:

std::vector<std::string> printable_cases = { "case1", "case2" };
BOOST_DATA_TEST_CASE(test_mvex, utdata::make(printable_cases), sample) {
    // Do some tests with sample
}

这不起作用:

struct Thingmajig {
    // I really don't care for printability!
    explicit Thingmajig(int a, int b) { c = a + b; }
    int c;
};
std::vector<Thingmajig> nonprintable_cases = { Thingmajig(1, 2), Thingmajig(4, 7) };
BOOST_DATA_TEST_CASE(test_mvex2, utdata::make(nonprintable_cases), sample) {
    // Do some tests with sample
}

它会出错:

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'const T' (or there is no acceptable conversion)    
    ...\boost\test\tools\detail\print_helper.hpp    54  
Error   C2338   Type has to implement operator<< to be printable     
  ...\boost\test\tools\detail\print_helper.hpp  52  

我们在 out 代码库中有很多类型不提供 operator&lt;&lt; 并且必须定义一个只是为了使数据测试用例的编译成为可能看起来很烦人。

这是 BOOST_DATA_TEST_CASE 如何从数据构造测试用例的限制,还是有一些解决方法?


前言:

  • 只需在单元测试文件本身中定义一个独立的裸骨输出操作符就足够了,该类型不需要全局/通用提供一个。当可印刷性与测试无关时,这仍然很烦人。
  • 我实际上在样本类型包含std::vectorstd::tuple 的情况下遇到了这个问题:对于这些标准库容器,cxx-prettyprint 在测试用例中是一个很好(足够)的解决方案。

【问题讨论】:

    标签: c++ boost boost-test


    【解决方案1】:

    确实BOOST_DATA_TEST_CASEcurrent implementation 要求参数是可打印的:在测试开始之前,BOOST_TEST_CONTEXT 使用当前测试参数在夹具中创建,以便 Boost.Test 框架可以记录/打印该特定参数集的消息(特别是打印精确的错误消息)。

    默认情况下,STL 容器没有默认打印,尽管 the logging customization 应该可以实现模板类的打印。

    【讨论】:

    • 谢谢。我注意到 Boost.Test 的 Logging 自定义与 cxx-prettyprint 中的问题相同,即它需要在 type's namespace, which is formally UB for std 中定义帮助函数
    • @MartinBa 感谢您的反馈!我将开一张票进行调查,还有其他一些关于该主题的管道。
    • 我相信this 应该可以解决这个问题。如果我需要创建另一个,请告诉我。
    • 就是这样。谢谢。现在我们只需要实现它:-)
    猜你喜欢
    • 2013-03-08
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2021-06-25
    相关资源
    最近更新 更多