【发布时间】: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<< 并且必须定义一个只是为了使数据测试用例的编译成为可能看起来很烦人。
这是 BOOST_DATA_TEST_CASE 如何从数据构造测试用例的限制,还是有一些解决方法?
前言:
- 只需在单元测试文件本身中定义一个独立的裸骨输出操作符就足够了,该类型不需要全局/通用提供一个。当可印刷性与测试无关时,这仍然很烦人。
- 我实际上在样本类型包含
std::vector和std::tuple的情况下遇到了这个问题:对于这些标准库容器,cxx-prettyprint 在测试用例中是一个很好(足够)的解决方案。
【问题讨论】:
标签: c++ boost boost-test