2

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:

  • Just defining a free-standing bare bones ouput-operator in the unit test file itself is obviously enough, the type does not need to provide one globally/generically. This is still annoying when printability is irrelevant for the test.
  • I actually hit this where the sample type contained a std::vector and a std::tuple: For these std lib containers, cxx-prettyprint is a good (-enough) solution inside the test cases.
Martin Ba
  • 37,187
  • 33
  • 183
  • 337

1 Answers1

1

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.

Raffi
  • 3,068
  • 31
  • 33
  • Thanks. I'd note that the Logging customization for Boost.Test suffers from the same problem than the one in cxx-prettyprint, namely that it requires the helper function to be defined in the [type's namespace, which is formally UB for `std`](https://stackoverflow.com/q/25120423/321013) – Martin Ba Jun 16 '20 at 22:17
  • @MartinBa thanks for the feedback! I'll open a ticket to investigate, there are some others in the pipeline on that subject as well. – Raffi Jun 17 '20 at 07:59
  • 1
    I believe [this](https://github.com/boostorg/test/issues/131) should solve the issue. Let me know if I need to create another one. – Raffi Jun 19 '20 at 20:06
  • This is exactly it. Thanks. Now we just need to implement it :-) – Martin Ba Jun 19 '20 at 20:17