Home |
[Previous: Chapter 1] [Contents] [Next: Chapter 3]
Files:
In this chapter we will demonstrate how to execute a test multiple times with different test data.
So far, we have hard coded the data we wanted to test into our test function. If we add more test data, the function might look like this:
COMPARE(QString("hello").toUpper(), QString("HELLO")); COMPARE(QString("Hello").toUpper(), QString("HELLO")); COMPARE(QString("HellO").toUpper(), QString("HELLO")); COMPARE(QString("HELLO").toUpper(), QString("HELLO"));
To prevent that the function ends up being cluttered by repetative code, QtTest supports adding test data to a test function. All we need is to add another private slot to our test class:
class TestQString: public QObject { Q_OBJECT private slots: void toUpper_data(QtTestTable &t); void toUpper(); };
A test function's associated data function carries the same name, appended by _data, and it takes a QtTestTable reference as an argument. Our data function looks like this:
void TestQString::toUpper_data(QtTestTable &t) { t.defineElement("QString", "string"); t.defineElement("QString", "result"); *t.newData("all lower") << "hello" << "HELLO"; *t.newData("mixed") << "Hello" << "HELLO"; *t.newData("all upper") << "HELLO" << "HELLO"; }
First, we define the two elements of our test table using the QtTestTable::defineElement() function: A test string, and the expected result of applying the QString::toUpper() function to that string.
Then we add some data to the table using the QtTestTable::newData() function. Each set of data will become a separate row in the test table.
QtTestTable::newData() takes one argument: A name that will be associated with the data set. If the test fails, the name will be used in the test log, referencing the corrupted data. Then we stream the data set into the new table row: First an arbitrary string, and then the expected result of applying the QString::toUpper() function to that string.
You can think of the QtTestTable as a two-dimensional table. In our case, it has two columns called string and result and three rows. In addition a name as well as an index is associated with each row:
index | name | string | result |
---|---|---|---|
0 | all lower | "hello" | HELLO |
1 | mixed | "Hello" | HELLO |
2 | all upper | "HELLO" | HELLO |
Our test function can now be rewritten:
void TestQString::toUpper() { FETCH(QString, string); FETCH(QString, result); COMPARE(string.toUpper(), result); }
The TestQString::toUpper() function will be executed three times, once for each entry in the QtTestTable that we created in the associated TestQString::toUpper_data() function.
First, we fetch the two elements of the data set using the FETCH() macro. FETCH() takes two arguments: The data type of the element and the element name. Then we perform the test using the COMPARE() macro.
This approach makes it very easy to add new data to the test without modifying the test itself.
And again, to make our test case a stand-alone executable, the following two lines are needed:
QTTEST_MAIN(TestQString) #include "testqstring.moc"
As before, the QTTEST_MAIN() macro expands to a simple main() method that runs all the test functions, and since both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.
[Previous: Chapter 1] [Contents] [Next: Chapter 3]
Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt Solutions |