|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

[Previous: Chapter 1] [Contents] [Next: Chapter 3]

# Chapter 2: Data Driven Testing

Files:

- qtestlib/tutorial2/testqstring.cpp

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:

     QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
     QCOMPARE(QString("Hello").toUpper(), QString("HELLO"));
     QCOMPARE(QString("HellO").toUpper(), QString("HELLO"));
     QCOMPARE(QString("HELLO").toUpper(), QString("HELLO"));

To prevent that the function ends up being cluttered by repetitive code, QTestLib supports adding test data to a test function. All we need is to add another private slot to our test class:

     ** This file is part of the example classes of the Qt Toolkit.
     **
     ** This file may be used under the terms of the GNU General Public
     ** License version 2.0 as published by the Free Software Foundation
     ** and appearing in the file LICENSE.GPL included in the packaging of
     ** this file.  Please review the following information to ensure GNU
     ** General Public Licensing requirements will be met:
     ** http://trolltech.com/products/qt/licenses/licensing/opensource/
     **
     ** If you are unsure which license is appropriate for your use, please
     ** review the following information:
     ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
     ** or contact the sales department at sales@trolltech.com.
     **
     ** In addition, as a special exception, Trolltech gives you certain
     ** additional rights. These rights are described in the Trolltech GPL
     ** Exception version 1.0, which can be found at
     ** http://www.trolltech.com/products/qt/gplexception/ and in the file
     ** GPL_EXCEPTION.txt in this package.
     **
     ** In addition, as a special exception, Trolltech, as the sole copyright
     ** holder for Qt Designer, grants users of the Qt/Eclipse Integration
     ** plug-in the right for the Qt/Eclipse Integration to link to
     ** functionality provided by Qt Designer and its related libraries.
     **
     ** Trolltech reserves all rights not expressly granted herein.
     **
     ** Trolltech ASA (c) 2007
     **
     ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
     ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     **
     ****************************************************************************/

     #include <QtTest/QtTest>

     class TestQString: public QObject
     {
         Q_OBJECT

     private slots:
         void toUpper_data();
         void toUpper();
     };

## Writing the Data Function

A test function's associated data function carries the same name, appended by `_data`. Our data function looks like this:

     void TestQString::toUpper_data()
     {
         QTest::addColumn<QString>("string");
         QTest::addColumn<QString>("result");

         QTest::newRow("all lower") << "hello" << "HELLO";
         QTest::newRow("mixed")     << "Hello" << "HELLO";
         QTest::newRow("all upper") << "HELLO" << "HELLO";
     }

First, we define the two elements of our test table using the QTest::addColumn() 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 QTest::newRow() function. Each set of data will become a separate row in the test table.

QTest::newRow() 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 failed 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 test data 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

## Rewriting the Test Function

Our test function can now be rewritten:

     void TestQString::toUpper()
     {
         QFETCH(QString, string);
         QFETCH(QString, result);

         QCOMPARE(string.toUpper(), result);
     }

The TestQString::toUpper() function will be executed three times, once for each entry in the test table that we created in the associated TestQString::toUpper_data() function.

First, we fetch the two elements of the data set using the QFETCH() macro. QFETCH() takes two arguments: The data type of the element and the element name. Then we perform the test using the QCOMPARE() 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:

     QTEST_MAIN(TestQString)
     #include "testqstring.moc"

As before, the QTEST_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]