gpt4 book ai didi

c++ - CPPUNIT:我们真的每次测试都需要一个函数吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:21:34 28 4
gpt4 key购买 nike

考虑这个 CPPUNIT 测试类打算进行相同的测试 ( doTest ) 但具有不同的参数:

class MyTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST( test1 );
CPPUNIT_TEST( test2 );
CPPUNIT_TEST( test3 );
CPPUNIT_TEST_SUITE_END();

public:
MyTest();

void test1() { doTest(1); }
void test2() { doTest(2); }
void test3() { doTest(3); }

void doTest( int param );
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTest);

有没有办法改变它以避免必须声明 test1 , test2test3 , 像这样:

class MyTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST_PARAM( doTest, 1 ); // CPPUNIT_TEST_PARAM does not exits, it's just to illustrate my need
CPPUNIT_TEST_PARAM( doTest, 2 ); // CPPUNIT_TEST_PARAM does not exits, it's just to illustrate my need
CPPUNIT_TEST_PARAM( doTest, 3 ); // CPPUNIT_TEST_PARAM does not exits, it's just to illustrate my need
CPPUNIT_TEST_SUITE_END();

public:
MyTest();

void doTest( int param );
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTest);

注意 CPPUNIT_TEST 是一个宏:

#define CPPUNIT_TEST( testMethod )                        \
CPPUNIT_TEST_SUITE_ADD_TEST( \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) )

编辑:

试过这个:

CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST( funcT<1> );
CPPUNIT_TEST_SUITE_END();

template<int i> void funcT() { doTest(i); }

它工作正常,但如果我使用 char* 会失败类型:

CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST( funcT<"foo"> );
CPPUNIT_TEST_SUITE_END();

template<char* s> void funcT() { std::cout << s << std::endl; doTest(1); }

错误:

error C2664: 'CppUnit::TestCaller<test_cppunit_regulation_regul_dt_100::TestFixtureType>::TestCaller(const CppUnit::TestCaller<test_cppunit_regulation_regul_dt_100::TestFixtureType> &)': cannot convert argument 2 from 'void (__cdecl *)(void)' to 'void (__cdecl test_cppunit_regulation_regul_dt_100::* )(void)'

或更多参数:

CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST( funcT<1,2> );
CPPUNIT_TEST_SUITE_END();

template<int i, int j> void funcT() { doTest(i+j); }

错误:

1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): warning C4002: too many actual parameters for macro 'CPPUNIT_TEST'
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2059: syntax error: ')'

最后尝试加括号(CPPUNIT_TEST( (funcT<1,2>) );),报错:

1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2589: '(': illegal token on right side of '::'
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2059: syntax error: '::'
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2660: 'CppUnit::TestSuiteBuilderContextBase::addTest': function does not take 2 arguments
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2143: syntax error: missing ';' before ')'
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\regul_dt_100\test.cpp(14): error C2059: syntax error: ')'

最佳答案

通过创建多个测试类(而不是一个包含多个子测试的单一测试类)找到了解决方案。

只有一个 int 参数的简单情况:

class BaseTest : public CPPUNIT_NS::TestFixture
{
public:
BaseTest() {}

void doTest( int param ) {}
};

template < int i >
class MyTest : public BaseTest
{
CPPUNIT_TEST_SUITE(MyTest<i>);
CPPUNIT_TEST( doTest );
CPPUNIT_TEST_SUITE_END();

void doTest()
{
BaseTest::doTest( i );
};
};

#define REGISTER_TEST_WITH_PARAMS( name, a ) \
CPPUNIT_TEST_SUITE_REGISTRATION( MyTest<a> );

REGISTER_TEST_WITH_PARAMS( test1, 1 );
REGISTER_TEST_WITH_PARAMS( test2, 2 );

如果需要更多的参数,只需要创建一个类来封装它们:

class BaseTest : public CPPUNIT_NS::TestFixture
{
public:
BaseTest() {}

void doTest( int param1, const std::string& param2 ) {}
};

class ParamClass
{
public:
ParamClass( int param1, const std::string& param2 ) :
param1( param1 ),
param2( param2 )
{

}

int param1;
std::string param2;
};

template < ParamClass & T >
class CURRENT_MODULE : public BaseTest
{
CPPUNIT_TEST_SUITE(MyTest<T>);
CPPUNIT_TEST( doTest );
CPPUNIT_TEST_SUITE_END();

void doTest()
{
BaseTest::doTest( T.param1, T.param2 );
};
};

#define REGISTER_TEST_WITH_PARAMS( name, a, b ) \
ParamClass name( a, b ); \
CPPUNIT_TEST_SUITE_REGISTRATION( MyTest<name> );

REGISTER_TEST_WITH_PARAMS( test1, 1, "test1" );
REGISTER_TEST_WITH_PARAMS( test2, 2, "test2" );

关于c++ - CPPUNIT:我们真的每次测试都需要一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40892648/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com