Chapter 5. Tutorial: Starting to Test Qt Applications

Table of Contents

5.1. Creating a Test Suite
5.2. Recording Tests and Verification Points
5.3. Inserting Additional Verification Points
5.3.1. Test Results
5.4. Creating Tests by Hand
5.4.1. Modifying and Refactoring Recorded Tests
5.4.2. Creating Data Driven Tests
5.5. Learning More

This tutorial will show you how to create, run, and modify tests for an example Qt application. In the process you will learn about Squish's most frequently used features so that by the end of the tutorial you will be able to start writing your own tests for your own applications.

This chapter presents most of the major concepts behind Squish and provides the information you need to get started using Squish for testing your own applications. This tutorial only covers a subset of Squish's features—including the most useful and frequently used ones—and those that it does cover are not covered in full detail. After reading this tutorial we recommend reading the User Guide (Chapter 15), and at least skimming the Tools Reference Manual (Chapter 16), so that you become familiar with all the features that Squish has to offer, even if you don't need to use them all straight away.

This tutorial is divided into several sections. If you are new to Squish (or to the new IDE introduced in Squish 4), it is best to read all of them. If you are already using Squish you might want to just skim the tutorial, stopping only to read those sections that cover any new features that you haven't used before—or you could just skip straight to the User Guide (Chapter 15).

Whenever we show how to achieve something using the IDE we will always follow with an explanation of how to do the same thing using the command line tools. Using an IDE is the easiest and best way to start, but once you build up lots of tests you will want to automate them, (e.g., doing nightly runs of your regression test suite), so it is worth knowing how to use the command line tools since they can be run from batch files or shell scripts. (Note that we use the new IDE introduced in Squish 4 throughout the tutorial. The old IDE is still packaged with Squish and so is still available; however, we recommend using the new IDE.)

For this chapter we will use a simple Address Book application as our AUT. The application is shipped with Squish in squish/examples/qt4/addressbook. This is a very basic application that allows users to load an existing address book or create a new one, add, edit, and remove entries, and save (or save as), the new or modified addressbook. Despite the application's simplicity, it has all the key features that most standard applications have: a menu bar with pull down menus, a toolbar, and a central area—in this case showing a table. It supports in-place editing and also has a pop-up modal dialog for adding items. All the ideas and practices that you learn to test this application can easily be adapted to your own applications. And naturally, the User Guide (Chapter 15) has many more examples and shows how to test lots of Qt-specific features including model/view models and views, as well as all the standard editing widgets.

The screenshot shows the application in action with a user adding a new name and address.

The Qt addressbook example.
[Note]Qt 2/Embedded and Qt 3 Users

We recommend that Qt 2/Embedded and Qt 3 users work through this tutorial using Qt 4. Doing so will lay all the conceptual groundwork and give you a good start at using Squish in practice. Then, you might like to look at some examples that are more specific to your Qt version, such as the squish/examples/qt3/addressbook example (which is essentially the same as the one covered here) and its accompanying tests since it is compatible with both Qt 2/Embedded and with Qt 3. And Qt 3 users might also find the squish/examples/qt3/canvas example to be helpful.

In fact, there are no fundamental differences of principle, and almost no differences of practice, between testing Qt 4 and Qt 3 applications. However, there are some differences in the resulting test scripts because of differences in class names and methods between the two Qt versions. For example, the Qt 3 version of the Address Book example uses the QTable class whereas the Qt 4 version uses the QTableWidget class, and these classes have slightly different APIs. Nonetheless, the test scripts for both the Qt 3 and Qt 4 Address Book examples are almost the same, with just two differences due to the different table widgets they use, and a few other differences due to different names being generated—again the result of different classes being used in the two AUTs.

[Note]Using the Examples

If you want to actively follow the tutorial, that is, actually run the tests, it is easiest to use the Qt libraries that came with Squish. This can be done by clicking the Window|Preferences menu option, then clicking the Squish item in the tree on the left, and then clicking the directory where the Squish package (not the Squish IDE package) is installed. You can always go through this process again at the end to set Squish to use the copy of the Qt libraries that your own applications use.

The first time you try running a test for one of the example AUTs you might get a fatal error that begins Squish couldn't find the AUT to start.... If this occurs, click the Test Suite Settings toolbar button, and in the Application Under Test (AUT) section choose the AUT from the combobox if it is available, or click the Browse... button and choose the AUT's executable via the file open dialog that pops up. This only needs to be done once per example AUT. (This doesn't arise when testing your own AUTs.)

In the following sections we will create a test suite and then create some tests. If you haven't already read Squish Concepts and Making an Application Testable (Chapter 4), it is strongly recommended that you do so now, since the rest of the tutorial assumes a knowledge of the basic concepts described in that (extremely short) chapter.