Monday, February 28, 2011

Swinging Test Suites with WindowTester

Arie van Deursen

One of the topics that popped up a couple of times in our "test confession" interviews with Eclipse developers, is the tension between unit testing and GUI testing. Does an application with a thorough unit test suite require user interface testing? And what about the other way around? What does automated GUI testing add to standard unit testing? Is automated GUI testing a way to involve the end-users in the testing process?

In order to fully understand all arguments, I decided to play around a little with WindowTester, a capture-and-playback automated GUI testing tool recently open-sourced by Google, with support for Swing and SWT.

My case study is JPacman, a Java implementation of a game similar to Pacman I use for teaching software testing. The plan of attack is simple: take the existing use cases, turn each into a click trail, record the trail, and generate a (JUnit-based) test suite.


My first use case is simple: enter and exit the game. To that end, I open the recorder, launch pacman, and press the red "Record" button in Eclipse. Then I press the start button in the game, followed by the exit button, which quits the application. WindowTester then prompts for the place to save this interaction:




After that, WindowTester generates the following JUnit test case:


public class StartAndExitUseCase extends UITestCaseSwing {

import ...

public StartAndExitUseCase() {
super(jpacman.controller.Pacman.class);
}
public void testStartAndExitUseCase() throws Exception {
IUIContext ui = getUI();
ui.click(new JButtonLocator("Start"));
ui.click(new JButtonLocator("Exit"));
ui.wait(new WindowDisposedCondition("JPacman"));
}
}

This test passes nicely.

The next use case requires me to make moves in several directions.
Unfortunately, WindowTester can't record arrow keys. To resolve this, I decide to modify Jpacman to use good old vi-navigation ('hjkl').
I then open JPacman, to make moves in all directions. Unfortunately, I'm a bit slow, and I bump into one of the randomly moving monsters, after which I die.
This is a deeper issue: Parts of the application, in particular the random monsters, cannot be controlled via the GUI. Without such control, it is impossible to have test cases with reproducible results.
My solution is to create a slightly different version of Pacman, in which the monsters don't move at all. In fact, I happened to have the code for this ready already in my test harness, as I used such a version for doing unit testing.

This works, and the result is a test case passing just fine:

public void testSimpleMove() throws Exception {
IUIContext ui = getUI();
ui.click(new JButtonLocator("Start"));
ui.enterText("jlkhhh");
}

The test doesn't assert much, though. Luckily, WindowTester has a mechanism to insert "hooks" while recording, prompting me for a name of the method to be called.


This results in the following code:

public void testSimpleMoveWithAsserts() throws Exception {
IUIContext ui = getUI();
ui.click(new JButtonLocator("Start"));
ui.enterText("l");
assertCorrectMoveToTheLeft();
ui.enterText("j");
assertCorrectMoveDown();
}

protected void assertCorrectMoveDown() throws Exception {
// TODO Auto-generated method stub
}

...

WindowTester generates empty bodies for the two assert methods, leaving it to the developer to insert appropriate code. This raises two issues.

The first is that the natural way (at least for me as a tester) to verify that a move down was conducted correctly is to ask the position of the player to the appropriate objects. But from the GUI, I don't have access to these. My work around is to adjust Pacman's "main" method, to make the underlying model available through a static reference. This results in the following code:

protected void assertCorrectMoveDown() {
Pacman pm = SimplePacman.instance();
assertEquals(1, pm.getEngine().getPlayer().getLastDy());
}

Writing such an assertion requires good knowledge of the underlying API, and a bit of luck that the API exposes a method to witness the desired effect.

My next use case involves a hungry Pacman consuming a dot, which earns the player 10 points. Doing this in a way similar to the previous use case iss simple enough. Would it also be possible to assert that the proper number of points are displayed correctly in the GUI? This requires getting hold of the appropriate JTextField, and checking its content before and after eating a dot.

To support this, WindowTester offers a number of widget locators. An example is the locator used above to find a JButton labeled with "Start". Other types of locators make use of patterns, the hierarchical position in the GUI, or a unique name that a developer can give to widgets. I use this latter option, allowing me to retrieve the points displayed as follows:

private int getPointsDisplayed() throws WidgetSearchException {
WidgetReference<JTextField> wrf =
(WidgetReference<JTextField>)
getUI().find(new NamedWidgetLocator("jpacman.points"));
JTextField pointsField = (JTextField) wrf.getWidget();
return Integer.parseInt(pointsField.getText());
}

Thus, I can test if the points actually displayed are correct.

The remaining use cases can be handled in a similar way. Some use cases require moving monsters, for which having access to the GUI alone is not enough. Another use case, winning the game, would require a lot of clever moves on the regular board: instead I create a custom small and simple board, in which winning is easy.

I less and less make use of the recording capabilities of WindowTester: instead I directly program against its API. This also helps me to make the test cases easier maintainable: I have a "setUp" for pushing the "Start" button, a "tearDown" for pushing "Exit", and I can make use of other JUnit best practices. Moreover, it allows me to create a small layer of methods permitting more abstract test cases, such as the method above to obtain the actual points displayed.

Are the resulting test cases useful? I recently changed some of the GUI logic, turning a complex if-then-else to handle keyboard events into a much cleaner switch statement (inspired by a warning PMD was giving me). All unit test cases passed fine. I almost committed, but then decided to run the GUI test suite as well. It failed. I first blamed WindowTester, but then I realized it was my own fault: I had forgotten some breaks in my switch and the events were not handled correctly. The GUI test suite found a fault my unit test suite had not found.

In summary, automated GUI testing is neither a replacement for unit nor for acceptance testing. It is a useful tool for covering the GUI logic of your application. The recording capabilities can be helpful to try out certain scenarios. In the end, however an explicitly programmed test suite, making use of the GUI framework's API, seems easier to maintain. In this way, JUnit best practices related to test suite organization as well as the application's observability and controllability can be directly applied to your GUI test suite as well.



I prefer steering GUI testing programmatically to working with a recorder.

Wednesday, February 16, 2011

GUI Testing in 23 Minds

During the Eclipse testing study, we ask participants about their experiences and their opinions on testing Eclipse Plug-ins and RCP applications. In this blog post, we want to share an excerpt of what some Eclipsers really think about automated GUI testing.

What the Adopters say:
Four participants out of 23 actively use GUI testing tools and have automated UI test suites.
P3 experiences that GUI testing is better suited to capture the customer's perspective, because “the tests can capture implicit assumptions and the workflow.” The company of P3 wrote their own tooling, since one fundamental problem of other tools is: “if you [as a tester] are not in the position that you can develop yourself, you always wait for something, or somebody.” Now, with their own testing tool, GUI testing is one of their strong points. P3 knows: “anything that will affect the user, we prefer to write as an acceptance test. [...] It’s a way of getting those discussions going.”

Also P11's team uses automated GUI tests. He reports: “One thing that is really good is the extension of GEF for the SWT-Bot. It is hard to test a graphical editor. We can test them very easily, and we are very happy about that.“

Still, not all are completely satisfied with the tooling. In P18's company they use SWT-Bot and also developed their own capture tool helping to develop test cases. P18 reports: “We haven't been 100% satisfied with the capture-replay, because too much is captured.” Their solution is: “after a capture, we always have a review to remove unnecessary code.”

Also P10’s company uses QF-Test to automate GUI tests. From his experience the maintenance of the test suites takes a lot of effort, he says: “it happens that during product evolution, suddenly something works differently and the tests do not work anymore. [There are] synchronization problems, sometimes the test has not been set-up in a clean way, or timing problems occur. To cope with that it takes a lot of time.”


What Non-adopters say:
All other participants do not invest in automated GUI testing. Some participants used it for some time, but they report that the investments are too high and the benefits too low. Maintenance problems and the disability to cope with evolution are two problems mentioned by many participants. P15 says: “In my experience, automated UI testing is very expensive with not a big benefit, especially [if you have a lot] of change, which makes it a high investment which might never lead to a benefit over running some manual tests. Although manual tests are manual, they tend to be more flexible and lead to better benefits.” And P14 says: “What we had was a QF-Test suite [...] but it became apparent that those [tests] are too rigid to use them further [if software evolves]. That's why we stopped using them.”

P21’s team developed in two projects GUI test suites, but now they discontinued.
His resume is: “The decision has been done mainly by us [developers]. We put an immense effort to write UI tests, [...] and in the end often there was more test code than code to test. I doubt that makes sense.” He also knows: “There has to be a strong commitment from the customer to invest in.”

Also recorders are not beloved by many developers. Participants report that “recorders are mindless” (P4), and “superficial” (P7). “When I use [those tools], I haven’t done the really hard work. [...] with all this investment, I still do not have what I want.” (P7).

The set-up and the configuration of the build environment to execute graphical user interface tests is experienced as a hard and time consuming task which requires a lot of knowledge and expertise.

What the Sceptics say:
Many participants see an alternative solution to automated GUI testing by keeping the GUI as small as possible and in decoupling the logic behind a GUI from the GUI code. P1 even thinks that “80% of the code are already tested with unit tests”, and also P16 says “we do not have UI tests, because we can cover that with unit tests.”

Conclusion
Benefits of automated UI testing reported in our study are the ability to bring in a customer perspective in the tests, foster discussions between developers and testers, and to cover code that is hard to test otherwise. The main risks of incorporating automated GUI testing recognized by the participants are the high effort needed to create and maintain the test suites, the unclear benefits compared to other types of tests, and the usage of immature tooling with inherent problems (e.g., timing, synchronization, set-up and configuration).


Do you have similar/different experiences? Which role has GUI testing in your daily practice? Please leave a comment!



Interested? More results? Come to our talk at EclipseCon. Come, see and share your experience.

Tuesday, December 14, 2010

More money for testing

Last Friday, I followed the webinar of James Whittaker, Testing expert @ Google, called “More bang for your testing buck”. Now, I want to summarize the talk but also reflect on the interviews of the Testing study. In addition, I’m eager to hear your opinion about this subject.

The main question is: If you have additional money to spend on testing, where would you invest and why?”
In his talk, James reasons about two options: Spending it early and let developers test, or spending it in later phases and have test professional test the product. He argues that quality cannot be tested in, and that developers are actually creating the product. Thus, they might be more valuable than testers. In a second thought, he mentions reduced costs for testing early in the cycle. When a developer sees a bug, he often does not even have to write a bug report, he just fixes it. The third reason why money might be invested during development is because testers write too many, as he calls it, useless test plans. Test plans get written, get outdated and die.

“Developers grow trees.” he says, and nobody knows a tree better than the developer who grew it. On the other hand, who is responsible for the forest, i.e., the integration of trees? Exactly this task should be performed by testers. The second reason to invest in testers is because they bring in the user perspective. The third reason why somebody should invest in testers is because many applications nowadays are just built by putting together pieces of software, without ever writing a single line of code.

James gave exactly three reasons for spending money during early testing and another three reasons why somebody should spend money in later phases. Nevertheless, after presenting this “fair-play” summary he showed a couple of nasty bugs. These bugs can be identified by smart human beings, but there is no way to automate finding such bugs a priori.
A picture of one of the presented bugs shows the walking road from Cambridge to Hull, unfortunately the walker must swim for a bit.
bugMapsSmall.PNG

All in all, he concludes that he would spend the money during later phases for manual testing of applications by “smart” testers, but that it is even more important to increase the productivity of testers by e.g., tooling. The time testers spend writing test plans has to be reduced. At Google, they use a tool for test planning called “Testify”. A second measurement to increase testers’ productivity is to reduce the time spend to file a bug. Also for this Google has a tool assisting testers.

After listening to this talk I am interested in how this measurements fit into the Eclipse world. The interviews of the Testing Study revealed that most of the commercial projects have dedicated testers on their team. Those indeed perform the tests on behalf of the customer, represent the user-perspective and mostly perform manual testing. On the other hand, nearly none of the open source projects have dedicated testers. Tests (automated and manual) are performed by developers themselves. In the interviews, some open source developers mention they are worried that they have a too strong technical perspective on testing, and might oversee the users' perspective. One might argue that many developers of open source projects are also their own clients and use their own software even on a daily basis.

I wonder: "Why do open source development teams not have testers on their teams but commercial projects do?" Do open source teams not need testers because “they eat their own dog food”? Is the community around a project to some degree responsible for manually testing? Or are commercial projects not up to date with their development strategies?

What do you think about this subject, and are you involved in an open source or in a commercial project?

Tuesday, December 7, 2010

Sneak Preview of the Study Results

For the last couple of months, more than 20 interviews with experienced Eclipse developers and testers have been conducted in order to get a deep understanding of the way testing is done within the Eclipse community. During the interviews, the participants have been asked about their general test process and strategy, but also about how they handle versioning, when and how they test the user interface, and which tools they use to facilitate testing. Participants revealed also the challenges they face during testing, how they handle them and which strategies they have identified as best practices in their projects.

The very good thing about such a study is that in contrast to the experience, preferences and dislikes of just one person or one project, more than 20 people could express their opinions and expertise, which gives the possibility to share best practices and pitfalls from different projects with different characteristics, needs and development strategies. Amongst others we interviewed people from following open source projects: EMF, SOA Platform, Mangrove, Usus, EclEmma, JaCoCo, IMP, CDO, GDA, Spoofax, RAP, Mylyn, GEF, and counting. But we also interviewed people involved in commercial products that we can not mention here, with the exception of GUIDancer and Tasktop.

To share all results with you, we submitted an EclipseCon proposal for 2011 called "Test Confessions: What Eclipsers Think and Do about Testing". If you are interested in this study, you can have a sneak preview (like a movie trailer) of a potential talk by looking at the slides. If we are one of the lucky ones that get accepted we will present the whole results next March 2011.

Some of the participants agreed to be named, some want to remain anonymous. At this point, I would like to thank ALL participants, including Sven Efftinge, Leif Frenzel, Markus Harringer, Marc Hoffmann, Alexandra Imrie, Lennart Kats, Ed Merks, Tracy Miranda, Adrian Mos, Benjamin Muskalla, Steffen Pingel, Aurelien Pupier, Holger Staudacher, Eike Stepper, and Jurgen Vinju for taking part in this study.

Saturday, November 27, 2010

Testing Eclipse RCP and Plug-in Applications

This blog post tackles the question "What is a must-have reading for a plug-in or RCP-apps developer when it comes to testing?"

Over the past couple of month, I collected more than 100 resources on testing Eclipse plug-ins and RCP applications. Today I want to share the 6 primary testing pointers, and summarize some of the most often stated recommendations on testing. This is a subjective collection, please feel free to add missing pointers in the comment section. On purpose, I spared links to specific utility tools or testing frameworks.

The main article and also starting point for further reading is the wiki entry on Automated Testing [1]. This article covers some basic information on how to run tests, suggests useful test utility frameworks and also presents a list of user interface (UI) test tools.
Another somewhat older article focuses on automating Eclipse PDE unit tests using Ant, and gives further pointers to articles about how to automate PDE tests [2].

From several documents on the web, as well as talks in Eclipse events like Eclipse Summit and EclipseCon, it is apparent that writing unit tests and having continuous integration/testing are recommended as a best practice for Eclipse development. As for example, the Architecture Council lists unit testing, and continuous integration under the top 10 project development practices [3].


Further Test Driven Development is one of the most discussed and recommended strategies when it comes to testing. There are several resources out there, I selected a TDD webinar by Kevin Taylor [4]. In his webinar he suggests to use the Model-View-Presenter pattern to separate concerns and thus the UI code from the logic behind, which in turn helps to reduce the code within the UI.

In addition I would like to include some pointers to books: First, the Eclipse specific book of Erich Gamma and Kent Beck “Contributing to Eclipse” [5], which explains how to contribute to Eclipse in a test driven way. Further, one very well known but general book on testing, by Binder, namely “Testing object-oriented systems: models, patterns, and tools”, provides several patterns that are handy for practical use [6].

What else has to be included in every Eclipse plug-in/RCP developer's reading list?
Missing something? Please leave a comment.

------------------------------------------ Edit:
I am happy that people responded to the post and extended my list of testing pointers.
Here the additional links from the comments:

"Agile Java(TM): Crafting Code with Test-Driven Development" by Jeff Langr.
"Effective Java" by Joshua Bloch.
"FAQ: How to build a product": http://wiki.eclipse.org/FAQ_How_do_I_create_an_Eclipse_product%3F
"Eclipse PDE Build - Tutorial": http://www.vogella.de/articles/EclipsePDEBuild/article.html
------------------------------------------

References:
[1]: http://wiki.eclipse.org/Automated_Testing
[2]: http://www.eclipse.org/articles/article.php?file=Article-PDEJUnitAntAutomation/index.html
[3]: http://wiki.eclipse.org/Architecture_Council/Top_Ten_Project_Development_Practices
[4]: http://live.eclipse.org/node/700
[5]: http://portal.acm.org/citation.cfm?id=961854
[6]: http://portal.acm.org/citation.cfm?id=338330

-------------------------------------------------------------

Tuesday, November 2, 2010

#1 Testing Proposition: Our Opinion Counts

This is the first of a series of posts aiming at sharing some thoughts on testing with you that are inspired by the interviews during the Eclipse testing study. I formulate the thoughts as statements allowing you to express your opinion. Do you agree/disagree? Have you experienced otherwise? Please feel free to use also the comments to express your opinion in some more words and to foster a discussion on this topic.

These statements must not express opinions and experiences from individual participants.

The first proposition is concerned with the point in time when testing should take place and stands in contrast to test driven development that propagates a test first approach. The proposition is concerned with the overhead caused by writing test code very early in the development phase.

Questions raised are: “Does focusing too much on testing immature components hinder further code development because the tests will need to be changed whenever the component is refactored or adjusted? Therefore, should test code evolve over time and with the maturity of the component under test? Or should be writing test code the first priority when developing new functionality?”

Now it's your turn. Do you agree or disagree with the following proposition?

Friday, October 22, 2010

Poster @ Eclipse Summit Europe

The Eclipse Testing Study is present at the Eclipse Summit Europe with a poster.
Click for a sneak preview of the Poster on Slideshare.

This poster is intended to promote the study on identifying and conceptualizing the testing culture of Eclipse within the community and to foster an open discussion on testing best practices and pitfalls.

An open discussion helps to understand common testing traditions within Eclipse, but also to reveal diverging testing forces. With this study, we are interested in conceptualizing the different testing forces, and to develop a framework of testing methodologies within Eclipse. This framework can not only give insights into state-of-the-art testing and leverage current best practices, but it can also be used to direct improvements of testing strategies and tools. The results of the study will be compiled and made available to the community.