是否可以在 boost::test 上使用自动注册的 BOOST_PARAM_TEST_CASE ?

2024-01-02

是否可以混合使用BOOST_AUTO_TEST_CASE and BOOST_AUTO_TEST_CASE_TEMPLATE宏与BOOST_PARAM_TEST_CASE以任何方式?我什至对实现这一目标的真正混乱的方式感兴趣。

必须手动构建所有测试用例似乎非常乏味。但是BOOST_PARAM_TEST_CASE机制非常有用,但只有在您有测试初始化​​函数时才有效,这反过来要求您必须使用手动测试用例构建。

是否有任何文档介绍如何自己连接到自动化系统,以便您可以提供自己自动注册的测试?

我现在使用的是boost 1.46。


我为此写了自己的支持,因为似乎确实没有任何好的支持。这需要 C++11decltype特征和::std::remove_const and ::std::remove_reference库方法来工作。

宏定义是宏定义的修改版本BOOST_FIXTURE_TEST_CASE and BOOST_AUTO_TEST_CASE macros.

您可以通过声明您的函数来使用它:

BOOST_AUTO_PARAM_TEST_CASE(name, begin, end)
{
    BOOST_CHECK_LT(param, 5);  // The function will have an argument named 'param'.
}

这是定义的标头BOOST_AUTO_PARAM_TEST_CASE macro:

#include <boost/test/unit_test_suite.hpp>
#include <boost/test/parameterized_test.hpp>
#include <type_traits>

#define BOOST_FIXTURE_PARAM_TEST_CASE( test_name, F, mbegin, mend )     \
struct test_name : public F {                                           \
   typedef ::std::remove_const< ::std::remove_reference< decltype(*(mbegin)) >::type>::type param_t; \
   void test_method(const param_t &);                                   \
};                                                                      \
                                                                        \
void BOOST_AUTO_TC_INVOKER( test_name )(const test_name::param_t &param) \
{                                                                       \
    test_name t;                                                        \
    t.test_method(param);                                               \
}                                                                       \
                                                                        \
BOOST_AUTO_TU_REGISTRAR( test_name )(                                   \
    boost::unit_test::make_test_case(                                   \
       &BOOST_AUTO_TC_INVOKER( test_name ), #test_name,                 \
       (mbegin), (mend)));                                              \
                                                                        \
void test_name::test_method(const param_t &param)                       \

// *******

#define BOOST_AUTO_PARAM_TEST_CASE( test_name, mbegin, mend )           \
   BOOST_FIXTURE_PARAM_TEST_CASE( test_name,                            \
                                  BOOST_AUTO_TEST_CASE_FIXTURE,         \
                                  mbegin, mend)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以在 boost::test 上使用自动注册的 BOOST_PARAM_TEST_CASE ? 的相关文章

随机推荐