c++boostboost-test

Does BOOST_DATA_TEST_CASE always require printability of the samples?


I'm trying to get some data-driven test cases running with BOOST_DATA_TEST_CASE and figured out the basics o far.

However, I noticed that the type that is used as sample input MUST be printable:

This will work:

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

This will NOT work:

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
}

It will error out with:

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  

We have lots of types in out codebase that don't supply operator<< and having to define one just to make the compilation of the data test case possible seems quite annoying.

Is this a limitation of how BOOST_DATA_TEST_CASE constructs the test case from the data, or is there some way around this?


Preliminary notes:


Solution

  • Indeed the current implementation of BOOST_DATA_TEST_CASE requires the parameters to be printable: before the test starts, a BOOST_TEST_CONTEXT is created in the fixture with the current test parameters, such that the Boost.Test framework can log/print messages for that specific set of parameters (in particular to print precise error message).

    Also by default there is no default printing for STL containers, although it should be possible to implement the printing for a template class though the logging customization.