PDA

View Full Version : Displaying GUI events with QtTest



gavingc
25th August 2009, 14:02
For all those that would like to see what is actually happening during their GUI tests while using QtTestLib (http://qt.nokia.com/doc/4.5/qtestlib-manual.html). All you have to do is show the widget and start an event loop at the points you want to see or after some sequence has happened. Note that you can interact with the widget during the event loops and change the test results. This could be used to auto replay a sequence you find yourself doing over and over during testing or create an executable demo....simply drop the following code into the two files indicated and load in QtCreator or build and run from the command line as you prefer.

tst_hello.cpp


#include <QtGui>
#include <QtTest/QtTest>

/**
* Provides a Hello Qt World Gui/Integration test.
*/
class tst_Hello : public QObject
{
Q_OBJECT

void eventLoop(const int msec);

private slots:
void testGUI();
};

/**
* The event loop.
*/
void tst_Hello::eventLoop(const int msec)
{
QEventLoop loop;
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.setSingleShot(true);
timer.start(msec);
loop.exec();
}

/**
* The test method.
*/
void tst_Hello::testGUI()
{
QLineEdit lineEdit;
lineEdit.show();
eventLoop(1200);

QTest::keyClicks(&lineEdit, "Hello Qt World");
eventLoop(1200);

QCOMPARE(lineEdit.text(), QString("Hello Qt World"));
}

QTEST_MAIN(tst_Hello)
#include "tst_hello.moc"



GuiTest.pro


TEMPLATE = app
TARGET = guitest
SOURCES += tst_hello.cpp
QT += testlib