我正在尝试使用 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
我们在代码库中有很多类型,它们不提供
operator<<
,而不得不定义一个类型只是为了使数据测试用例的编译变得很烦人。这是否是BOOST_DATA_TEST_CASE如何从数据构造测试用例的限制,还是有解决办法?
初步说明:
std::vector
和std::tuple
的情况下遇到了这个问题:对于这些std lib容器,cxx-prettyprint是测试用例中很好的(足够的)解决方案。 最佳答案
实际上BOOST_DATA_TEST_CASE
的current implementation要求参数是可打印的:在测试开始之前,使用当前测试参数在夹具中创建了BOOST_TEST_CONTEXT
,以便Boost.Test框架可以记录/打印该特定参数集的消息(在特别是打印精确的错误消息)。
同样默认情况下,没有默认的STL容器打印,尽管应该可以通过the logging customization为模板类实现打印。
关于c++ - BOOST_DATA_TEST_CASE是否始终要求 sample 的可打印性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62211011/