Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Basic QtTest Lib Example

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Platform(s): All Qt Supported
Keywords: QtTest
Created: skumar_rao (26 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


This article shows how to convert string to it uppercase using Qt's QString. We'll start by writing a simple test to verify that the function QString::toupper correctly copy the string and convert it to uppercase.

Class Implementation


#include <QtTest>
class TestQString: public QObject
private slots:
void toUpper();
void TestQString::toUpper()
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));

Note that

  • The test class must inherit from QObject.
  • The tests must be declared as "private slots", so that QtTest can automatically recognize and execute them.
  • The macro QCOMPARE verifies that the two objects passed as arguments are identical and displays the value of these two objects, which facilitates debugging.
  • QCOMPARE macro is quite strict, in way that the objects compared must be the same type. if you pass a argument as QString and another as constant then a compiler error will occur
  • The macro QTEST_MAIN generates a main function that runs all the tests that have been reported via "private slots"

For compilation with qmake, just add the .pro for your project:

QT += testlib

more info can be found at

--skumar_rao 17:50, 26 November 2010 (UTC)

This page was last modified on 11 October 2012, at 01:16.
72 page views in the last 30 days.