Namespaces
Variants
Actions
Revision as of 04:16, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Basic QtTest Lib Example

Jump to: navigation, search
Article Metadata

Tested with
Devices(s): Emulator / desktop / device

Compatibility
Platform(s): All Qt Supported

Article
Keywords: QtTest
Created: skumar_rao (26 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Overview

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

testqstring.cpp

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

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 http://doc.qt.nokia.com/latest/qtestlib-manual.html

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

295 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved