考慮這個意思做同樣的測試(doTest
),但使用不同的參數CppUnit的測試類:CPPUNIT:我們真的需要每個測試一個功能嗎?
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); }'幫忙呢? –
我需要三個測試分開,讓他們都跑(如果你的建議,如果第一次失敗,最後兩個沒有執行) – jpo38
Look [here](http://stackoverflow.com/questions/5687540 /非A型模板參數#5687553)。你不能在那裏使用'std :: string',但'char *'可以工作。這是一個語言規範。你可以使用lambda發佈整個堆棧(我忘了在我的答案中保護它,現在修復)。 – kabanus