【发布时间】:2016-11-30 15:53:40
【问题描述】:
考虑这个 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、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, 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: ')'
【问题讨论】:
-
我不知道 cppunit 的惯用用法,但
void test1() { doTest(1); doTest(2); doTest(3); }有帮助吗? -
我需要将三个测试分开运行(根据您的建议,如果第一个失败,则不执行最后两个)
-
看here。你不能在那里使用
std::string,但char*会起作用。这是一个语言规范。您能否发布使用 lambda 的完整堆栈(我忘记在我的答案中保护它,现在已修复)。 -
@kabanus:在您的帖子中添加了完整的错误消息作为编辑。