Channels ▼

Pablo Santos

Dr. Dobb's Bloggers

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

June 16, 2014

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.

Here's a code snippet:

            ITesteablePendingChangesView pendingChangesView =
                testeablePlastic.GetTesteablePendingChangesView();

            pendingChangesView.CollapseRow(0);
            pendingChangesView.ExpandRow(0);
            pendingChangesView.ClickCheckboxAtRow(0);
            pendingChangesView.AddComment(comment);
            pendingChangesView.ClickCheckinButton();

How Does It Look and Feel?

Since one video is worth a thousand pictures, check out this screencast recorded on a Mac running an initial GUI test suite.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video