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,test2和test3,如下所示:
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,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 arguments1>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:每次测试我们真的需要一个功能吗?全部内容,希望文章能够帮你解决c – CPPUNIT:每次测试我们真的需要一个功能吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)