|
CS205 - Lab 18
|
|
Lab Goal:
Learn to do testing using the QtTest library that allows normal tests and GUI testing.
|
|
Basic Unit Testing:
The following discussion will walk you through the basics of setting up unit testing in kdevelop.
- Start up kdevelop and create a new project using: C++ -- QMake project -- Qt4 Application
- First thing, compile and run the application that this project will provide you using the rebuild and execute subproject buttons.
- Now create a test subproject called "test". Open the subproject settings. Click the QtTest check box under the Qt4 Libraries section of the Configuration tab, and close the dialog.
- Create a file called TestQString.cpp and load the following code into the file.
#include <QtTest/QtTest>
class TestQString: public QObject
{
Q_OBJECT
private slots:
void toUpper();
};
void TestQString::toUpper()
{
QString str = "Hello";
QVERIFY(str.toUpper() == "HELLO");
QCOMPARE(str.toUpper(), QString("HELLO"));
}
QTEST_MAIN(TestQString);
#include "TestQString.moc"
You can notice the following thing about the above code.
- You need the included test file at the top.
- QtTest uses Qt slot mechanism, so all tests should be placed just below the private slots:
- There are two macros for data driven testing, which can be read about here.
- The last two lines will build and create a simple main function that will run all your tests.
- Set up to run the test in the external terminal from the Project Options.
- Now give it at try using the rebuild and execute subproject buttons.
- If you are having problems compiling, there seems to an
occasional bug. Open the subproject configuration dialog, go
into the Custom Configuration text box and remove
"qt_no_framework", close the dialog, and do a complete rebuild
of the subproject.
|
|
Advanced GUI Unit Testing:
The following discussion will walk you through the basics of setting up unit testing in kdevelop. Note, a more detailed discussion can be found here and here.
- The QtTest library allows you to produce different key and button effects once you have access to a specific gui widget. You can do this by either inheriting the gui component you have created or instantiate the gui component and the drive the associated widgets.
- Do create a gui test, create a separate subproject called "testgui" for the previously generated project and create the file "TestGui.cpp"
- Add the following code to the new file:
#include <QtTest/QtTest>
#include <QtCore>
#include <QtGui>
class TestGui: public QObject
{
Q_OBJECT
private slots:
void testChanges();
};
void TestGui::testChanges()
{
// setup the current date
QDate date( QDate::currentDate() );
QDateEdit dateEdit( QDate::currentDate() );
// up-arrow should increase month by one
QTest::keyClick( &dateEdit, Qt::Key_Up );
QCOMPARE( dateEdit.date(), date.addMonths(1) );
QTest::keyClicks( &dateEdit, "122505" );
QCOMPARE( dateEdit.date(), QDate( 2005, 12, 25 ) );
QTest::keyClick( &dateEdit, Qt::Key_Tab, Qt::ShiftModifier );
QTest::keyClicks( &dateEdit, "08" );
QCOMPARE( dateEdit.date(), QDate( 2005, 12, 8 ) );
}
QTEST_MAIN(TestGui);
#include "TestGui.moc"
You can notice the following thing about the above code.
- The needed include files have been added.
- There are a series of "QTest::" based actions that all various keyclicks and mouseclicks. The various events are described here.
- After various mouseclick and keyclick have been performed, then various data driven checks are performed.
- Run and execute the code to see it function.
|
|
Assignment:
- Take your gui from lab 13 and add a data driven unit testing of the gui components.
|
|
Submission
Submit via Moodle. Your project for this lab should be called "lab18-submission".
Before submission, please make sure that your project can compile and run on the compute machines.
|
|
|
Created: Prof. CW Liew; Modified: Prof. Xiaoyan Li
Prof. Pfaffmann (pfaffmaj), Lafayette College, Last Modified: March 28, 2010
|