I saw that Qt supports a data function associated to a test function.
http://qt-project.org/doc/qt-4.8/qtestlib-tutorial2.html
Is it possible to have some similar type of data function for multiple tests ?
Example:
void Test::Test1()
{
SomeClass::SomeDataType a;
a.manyValuesComplicatedToSet = 1;
SomeOtherClass::SomeOtherDataType b;
b.manyValuesComplicatedToSet = 2;
QVERIFY(SomeTestClass::someFunction(a,b)== 3);
}
void Test::Test2()
{
SomeClass::SomeDataType a;
a.manyValuesComplicatedToSet = 1;
SomeOtherClass::SomeOtherDataType b;
b.manyValuesComplicatedToSet = 2;
QVERIFY(SomeTestClass::someOtherFunction(a,b)== 5);
}
I would love to be able to set the data above in a common data function, so that I would not type it all every time.
Is that possible ?
It's possible by extracting your test data into a separate function and then calling that function from your _data
functions:
void Test::Test1()
{
QFETCH(SomeClass::SomeDataType, a);
QFETCH(SomeOtherClass::SomeOtherDataType, b);
QCOMARE(SomeTestClass::someFunction(a,b), 3);
}
void Test::Test1_data()
{
createTestData();
}
void Test::Test2()
{
QFETCH(SomeClass::SomeDataType, a);
QFETCH(SomeOtherClass::SomeOtherDataType, b);
QCOMPARE(SomeTestClass::someOtherFunction(a,b), 5);
}
void Test::Test2_data()
{
createTestData();
}
void Test::createTestData()
{
QTest::addColumn<SomeClass::SomeDataType>("a");
QTest::addColumn<SomeOtherClass::SomeOtherDataType>("b");
SomeClass::SomeDataType a;
a.manyValuesComplicatedToSet = 1;
SomeOtherClass::SomeOtherDataType b;
b.manyValuesComplicatedToSet = 2;
QTest::newRow("test 1") << a << b;
}
Note that createTestData()
in my example above is not defined as a slot. Also note that to pass SomeClass::SomeDataType
and SomeOtherClass::SomeOtherDataType
as test data parameters, you must call Q_DECLARE_METATYPE
on them.