Pablo Santos

Dr. Dobb's Bloggers

Don't Develop GUI Tests, Teach Your App To Test Itself!

While developing native GTK- and Cocoa-based versions of the Plastic GUI, my team and I faced the challenge of developing a new test suite.

How Does It Work?

The basics are very simple: The tested application will be the one running the test code; that's why it is auto-tested. The basic is structure is as follows:

There are two main concepts:

The application thread is the main GUI thread, the one handling events. This thread executes the "real app code" and potentially collaborates with other worker threads used in the app (depending on the design). This code shouldn't be affected by the test code. It should run as if it was being used by a human user.

The test thread is the one executing the test code. It will interact with the app simulating a real user, but of course driven by the test code. As the image shows, the test code will eventually perform actions like a PerformClick() to run a click on a button or any other control, and in order to do that, it will interact with the application thread using invokes (in the C# world).

Please note that the test code won't invoke the underlying code that the OK button runs; it will perform a click on the OK button instead, which is the way we have to simulate user actions.

Now the question is how the test code discovers what it can do with a certain window or dialog. Take a look at the following figure:

Here comes the trick. Each window will expose an ITesteable interface, which is the one that the test code will use to perform actions on it.

In fact, each dialog will extend ITesteable to provide specific functionalities. The Main Window will provide options to navigate different menus (or the menus with the clickable options themselves), some action buttons, and so on. The Create Branch Dialog in the figure will provide ways to get and set the text on the two edit boxes, and a way to click the OK and Cancel buttons.

Of course, each window and dialog will have to be ready to be testable, but we think that is a small price to pay. At the end of the day, code must be testable in order to write unit tests, too.

The second advantage is that, on paper, the same testing code will be valid for the two GUIs: the "create branch dialog" will look different and will use different underlying controls on Mac and GTK. Layouts can be different, styles, fonts, and so on. But at the end of the day, the two will let you click on OK and Cancel and get and set a few text boxes. So if the two implement the same ITesteable interface (actually a child interface adding more features), the two will be able to be tested with the same code, which is a great advantage for us.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!