c++ - 具有夹具支持的 BOOST_DATA_TEST_CASE

标签 c++ boost boost-test

我正在 BOOST_DATA_TEST_CASE 中寻找夹具支持。我为它编写了以下 C++ 宏,但也许有人有更好的东西?

#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>

#define BOOST_FIXTURE_DATA_TEST_CASE_IMPL( arity, test_name, F, dataset, params )  \
struct test_name : public F {                                                      \
    template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>                            \
    static void test_method( BOOST_DATA_TEST_CASE_PARAMS( params ) )               \
    {                                                                              \
        BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture entry.");           \
        test_name t;                                                               \
        BOOST_TEST_CHECKPOINT('"' << #test_name << "\" entry.");                   \
        BOOST_TEST_CONTEXT( ""                                                     \
            BOOST_PP_SEQ_FOR_EACH(BOOST_DATA_TEST_CONTEXT, _, params))             \
        t._impl(BOOST_PP_SEQ_ENUM(params));                                        \
        BOOST_TEST_CHECKPOINT('"' << #test_name << "\" exit.");                    \
    }                                                                              \
private:                                                                           \
    template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>                            \
    void _impl(BOOST_DATA_TEST_CASE_PARAMS( params ));                             \
};                                                                                 \
                                                                                   \
BOOST_AUTO_TU_REGISTRAR( test_name )(                                              \
    boost::unit_test::data::ds_detail::make_test_case_gen<test_name>(              \
          BOOST_STRINGIZE( test_name ),                                            \
          __FILE__, __LINE__,                                                      \
          boost::unit_test::data::make(dataset) ),                                 \
    boost::unit_test::decorator::collector::instance() );                          \
                                                                                   \
template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>                                \
void test_name::_impl( BOOST_DATA_TEST_CASE_PARAMS( params ) )                     \
/**/

#define BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS( test_name, dataset, ... )          \
    BOOST_FIXTURE_DATA_TEST_CASE_IMPL( BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),       \
                               test_name, BOOST_AUTO_TEST_CASE_FIXTURE, dataset,  \
                               BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) )            \
/**/
#define BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS( test_name, dataset )       \
    BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS( test_name, dataset, sample ) \
/**/

#if BOOST_PP_VARIADICS_MSVC

#define BOOST_AUTO_DATA_TEST_CASE( ... )                                     \
    BOOST_PP_CAT(                                                            \
    BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2),      \
                     BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS,                    \
                     BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS) (__VA_ARGS__), ) \
/**/
#else

#define BOOST_AUTO_DATA_TEST_CASE( ... )                                     \
    BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2),      \
                     BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS,                    \
                     BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS) (__VA_ARGS__)    \
/**/
#endif

BOOST_DATA_TEST_CASE 的示例用法:

#include <boost/range/iterator_range.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

struct fixure
{
    fixure() :
        _Mydir(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("test-%%%%-%%%%-%%%%-%%%%"))
    {
        create_directory(_Mydir);
    }

    ~fixure()
    {
        _TRY_BEGIN
            remove_all(_Mydir);
        _CATCH_ALL
        _CATCH_END
    }

    boost::filesystem::path const _Mydir;
};

BOOST_FIXTURE_TEST_SUITE(suite, fixure)

namespace {

std::array<char const *, 4> const _Ourdata = { "A", "B", "C", "D" };

}

BOOST_AUTO_DATA_TEST_CASE(test, _Ourdata, _Str)
{
    BOOST_REQUIRE(_Str);
    BOOST_REQUIRE(is_directory(_Mydir));
    auto const _File = _Mydir / _Str;
    BOOST_TEST_MESSAGE(_File);
    std::fstream _Stream;
    _Stream.open(_File.c_str(), std::ios::out);
    _Stream << _Str;
    _Stream.close();
    BOOST_REQUIRE(is_regular_file(_File));
}

BOOST_AUTO_TEST_SUITE_END()

输出test_suite模式:

Running 4 test cases...
Entering test module "example"
Entering test suite "suite"
Entering test case "test"
"C:\Users\XXX\AppData\Local\Temp\test-4445-a983-8ba6-09ef\A"
Failure occurred in a following context:
    _Str = A;
Leaving test case "test"; testing time: 17ms
Entering test case "test"
"C:\Users\XXX\AppData\Local\Temp\test-d4c4-c5f6-6154-7200\B"
Failure occurred in a following context:
    _Str = B;
Leaving test case "test"; testing time: 10ms
Entering test case "test"
"C:\Users\XXX\AppData\Local\Temp\test-9f96-4f2c-b132-c541\C"
Failure occurred in a following context:
    _Str = C;
Leaving test case "test"; testing time: 9ms
Entering test case "test"
"C:\Users\XXX\AppData\Local\Temp\test-f0cf-962c-aed3-1cf8\D"
Failure occurred in a following context:
    _Str = D;
Leaving test case "test"; testing time: 10ms
Leaving test suite "suite"; testing time: 61ms
Leaving test module "example"; testing time: 76ms

*** No errors detected

更新:测试套件已更新为 std::array<>使用

最佳答案

从 Boost 1.62 开始,您可以使用 BOOST_DATA_TEST_CASE_F 来实现此目的:

Declares and registers a data-driven test case, using a particular dataset and a fixture.

BOOST_DATA_TEST_CASE_F(my_fixture, test_case_name, dataset, var1, var2..., varN)

引用:

https://www.boost.org/doc/libs/1_62_0/libs/test/doc/html/boost_test/utf_reference/test_org_reference/test_org_boost_test_dataset_fixture.html

关于c++ - 具有夹具支持的 BOOST_DATA_TEST_CASE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359691/

相关文章:

c++ - 在 MFC 中访问多个编辑框

c++ boost asio超时以阻止连接

c++ - Boost 单元测试在 Visual Studio 2012 中抛出异常

c++ - 如何预期 static_assert 失败并使用 Boost.Test 框架处理它?

c++ - c++ move 语义是否在每种情况下都节省了资源?

c++ - QT 的 AfxGetApp()->m_lpCmdLine?

C++ - 绑定(bind)函数

c++ - 如何使用 boost odeint 处理过去的时间偏移?

c++ - 无法从伪终端读取

c++ - move 构造函数的奇怪编译错误