5

The examples in the documentation of Boost.Test seem to be mostly refering to C++03 code.

I wonder if there is a more C++14/C++17-way of putting together the BOOST_TEST_CASEs in init_unit_test_suite?

This is what I derived from the documentation. I only exchanged boost::bind with lambdas. I can not think of any further "modernization" of that code. Can anyone?

Intro:

#include <boost/test/included/unit_test.hpp>
#include <memory> // shared ptr

using namespace boost::unit_test;
using namespace std::literals::string_literals;

Test class:

class test_class {
public:
    void test_method1() {
        BOOST_CHECK( true /* test assertion */ );
    }
    void test_method2() {
        BOOST_CHECK( false /* test assertion */ );
    }
};

Building the suite:

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    auto tester = std::make_shared<test_class>();
    auto &ts = framework::master_test_suite();
    ts.add( BOOST_TEST_CASE( [=](){ tester->test_method1(); } ));
    ts.add( BOOST_TEST_CASE( [=](){ tester->test_method2(); } ));
    return nullptr;
}
towi
  • 21,587
  • 28
  • 106
  • 187
  • 3
    I'd figure out a way to get rid of the `.add` calls and use `{}`s to bulk-add at least. But this may be more on topic at code review; go over there and check if it meets their standards. – Yakk - Adam Nevraumont Aug 23 '16 at 18:51
  • pinging @GennadiyRozental for adding initializer_list support :) – TemplateRex Aug 26 '16 at 09:19
  • and also @Raffi as the other Boost.Test maintainer – TemplateRex Aug 26 '16 at 09:22
  • The question that comes to my mind would be "why do you want to use a test class"? If you want to share a state between several test cases, best would be to use a fixture. Otherwise there is little a test class adds. – Raffi May 02 '20 at 11:36

0 Answers0