由@Omnifarious提供解決方案的工作工作,但需要C++ 11編譯器。
適應用於C++ 03編譯器溶液:
#include <boost/test/unit_test_suite.hpp>
#include <boost/test/parameterized_test.hpp>
#define BOOST_FIXTURE_PARAM_TEST_CASE(test_name, F, P, mbegin, mend) \
struct test_name : public F \
{ \
typedef P param_t; \
void test_method(const param_t &); \
}; \
\
void BOOST_AUTO_TC_INVOKER(test_name)(const test_name::param_t ¶m) \
{ \
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 ¶m) \
// *******
#define BOOST_AUTO_PARAM_TEST_CASE(test_name, param_type, mbegin, mend) \
BOOST_FIXTURE_PARAM_TEST_CASE(test_name, \
BOOST_AUTO_TEST_CASE_FIXTURE, \
param_type, \
mbegin, mend)
這種解決方案略有不同是使用情況。由於C++ 03中沒有declspec
,因此無法自動推導參數對象的類型。我們必須把它作爲一個參數BOOST_AUTO_PARAM_TEST_CASE
:
class FooTestParam
{
public:
std::string mS;
FooTestParam (int n)
{
std::stringstream ss;
ss << n;
mS = ss.str();
}
};
FooTestParam fooParams [] =
{
FooTestParam (42),
FooTestParam (314)
};
BOOST_AUTO_PARAM_TEST_CASE (TestFoo, FooTestParam, fooParams, fooParams + 2)
{
const std::string testVal = param.mS;
}
BOOST_AUTO_TEST_CASE (TestAddressField)
{
const uint32_t raw = 0x0100007f; // 127.0.0.1
const uint8_t expected[4] = {127, 0, 0, 1};
const Mdi::AddressField& field = *reinterpret_cast <const Mdi::AddressField*> (&raw);
for (size_t i = 0; i < 4; ++i)
BOOST_CHECK_EQUAL (field[i], expected[i]);
}
沒有人對'boost :: test'的內部工作有太多瞭解的是StackOverflow? – Omnifarious