Рассмотрим этот тест CppUnit класс означал, чтобы сделать тот же тест (doTest
), но с разными аргументами: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())))
Edit:
Пробовал так:
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); }
It ошибки:
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); } 'help вообще? –
Мне нужно, чтобы три теста были отдельными, чтобы их всех запускали (с вашим предложением, если первое не удается, два последних не выполняются) – jpo38
Посмотрите [здесь] (http://stackoverflow.com/questions/5687540/не тип-шаблон-параметры # 5687553). Вы не можете использовать 'std :: string', но' char * 'будет работать. Это спецификация языка. Не могли бы вы опубликовать полный стек с помощью лямбда (я забыл защитить его в своем ответе, исправленном сейчас). – kabanus