QtTestLib Qt Unit Testing Library Harald Fernengel - - PowerPoint PPT Presentation

qttestlib
SMART_READER_LITE
LIVE PREVIEW

QtTestLib Qt Unit Testing Library Harald Fernengel - - PowerPoint PPT Presentation

QtTestLib Qt Unit Testing Library Harald Fernengel <harald@trolltech.com> What Is It? Lightweight unit testing library Cross-platform, cross-compiler Tests are written in C++ Tests are stand-alone executables Features


slide-1
SLIDE 1

QtTestLib

Qt Unit Testing Library Harald Fernengel <harald@trolltech.com>

slide-2
SLIDE 2

What Is It?

  • Lightweight unit testing library
  • Cross-platform, cross-compiler
  • Tests are written in C++
  • Tests are stand-alone executables
slide-3
SLIDE 3

Features

  • Data-driven testing
  • Basic GUI testing
  • Qt Signal/Slot introspection
  • IDE integration (KDevelop,

VS)

slide-4
SLIDE 4

Hello World Test

#include <QtTest/QtTest> class QStringTest: public QObject { Q_OBJECT private slots: void toUpper() { QString str = "text"; COMPARE(str.toUpper(), QString("TEXT")); } }; QTTEST_MAIN(QStringTest)

slide-5
SLIDE 5

Building it

  • Run qmake -project CONFIG+=qttest
  • qmake && make
slide-6
SLIDE 6

Macros

  • VERIFY - Verifies that the condition is true:

VERIFY(i + j == 6);

  • COMPARE - Compares two values:

COMPARE(i + j, 6);

slide-7
SLIDE 7

Data-Driven Testing I

  • Run a test multiple times with different data:

void toUpper_data(QtTestTable &t) { t.defineElement("QString", "string"); t.defineElement("QString", "result"); *t.newData("lower") << "kde" << "KDE"; *t.newData("mixed") << "KdE" << "KDE"; }

slide-8
SLIDE 8

Data-Driven Testing II

  • The same test, this time data-driven:

void toUpper() { FETCH(QString, string); FETCH(QString, result); COMPARE(string.toUpper(), result); }

slide-9
SLIDE 9

Benefits

  • Separation of logic and data
  • Improved readability
  • Easily extendable
  • Eases testing of border cases
  • Reduces copy-paste code in tests
slide-10
SLIDE 10

GUI Testing

  • Keyboard and Mouse simulation
  • Sends Qt events (no X11 events)
  • Supports clicking, double-clicking, pressing

and releasing of keys and mouse movement

slide-11
SLIDE 11

GUI Testing Example

void testGui() { QLineEdit lineEdit; QtTest::keyClicks(&lineEdit, "hi KDE"); COMPARE(lineEdit.text(), "hi KDE"); }

slide-12
SLIDE 12

GUI Testing: Mouse

  • mouseClick(), mousePress() and

mouseRelease() all take:

  • a widget
  • a mouse button
  • an optional modifier (Shift/Ctrl/Alt)
  • a position (default: center of widget)
  • an optional delay
slide-13
SLIDE 13

GUI Testing: Keys

  • keyClick(), keyPress() and

keyRelease() all take:

  • a widget
  • a char or a Qt::Key
  • an optional keyboard modifier
  • an optional delay
slide-14
SLIDE 14

GUI Testing: Testdata

  • GUI events can be recorded:

void guiTest_data(QtTestTable &t) { t.defineElement("QtTestEventList", "e"); QtTestEventList list; list.addKeyClick('a'); list.addKeyClick(Qt::Key_Backspace); *t.newData("there and back") << list; }

slide-15
SLIDE 15

GUI Testing: Replay

  • A QtTestEventList can be replayed multiple

times:

void guiTest() { FETCH(QtTestEventList, e); QLineEdit lineEdit; e.simulate(&lineEdit); VERIFY(lineEdit.text().isEmpty()); }

slide-16
SLIDE 16

Signal introspection

  • QSignalSpy is useful to introspect signals:

QCheckBox box; QSignalSpy spy(&box, SIGNAL(clicked(bool)); box.animateClick(); COMPARE(spy.count(), 1); QList<QVariant> arguments = spy.takeFirst(); COMPARE(arguments.at(0).toBool(), true);

slide-17
SLIDE 17

QSignalSpy

  • QSignalSpy can connect to any signal from

any QObject

  • It can handle any kind of parameter as long as

it is registered with QMetaType

  • It is implemented as a list of list of QVariant
  • It "fakes" slots at runtime, heavily misusing

Qt's meta object system.

slide-18
SLIDE 18

Test Output

  • Output goes to stdout
  • Outputs plain text or XML
  • Supports colored output
  • Messages are atomar and thread-safe
  • IDE-friendly output
  • Verbose output, Signal/Slot dumper
slide-19
SLIDE 19

Other Good Stuff

  • EXPECT_FAIL - Marks the next VERIFY/

COMPARE as expected failure

  • SKIP - Skips the test and outputs a message
  • VERIFY2 - Verbose VERIFY
  • ignoreMessage() - swallows debug/warn

messages

slide-20
SLIDE 20

Summary

  • Universal toolbox for testing Qt code
  • Lightweight - 6000 LOC, 60 symbols

➙ Easy to learn, easy to maintain

  • Tests in C++, standard executables

➙ No special environment/task-switch needed

  • Self-contained, cross-platform, cross compiler

➙ Runs everywhere Qt does

slide-21
SLIDE 21

That's It

Questions?