Introduction to UI Unit Testing with UI Automation (
Page 1 of 8 )
The .NET framework includes some handy classes to help make your Unit testing easier. Brian Hunter shows you how.
In my experience, creating unit tests against application code has never been the most exciting aspect of development. Over time, I have learned the necessity of building effective tests that exercise most, if not all, of the code and logic. Unit tests certainly help in user acceptance by eliminating errors before the user sees the application. Tools such as NUnit and Microsoft’s Unit Testing Framework have made automated unit tests easier to implement.
One area of testing that has frustrated me is the user interface. I can write unit tests to validate methods of business objects without much trouble, but how do I validate the user interface? Normally, this comes from going through a manual sequence of user actions, attempting to duplicate how a user would interact with the application. Having an actual user test the application is helpful, but that requires an available resource. I would like to be able to have automated testing of the user interface, similar to the automated unit tests.
ADVERTISEMENT
Enter the Microsoft UI Automation framework. While recently delving into Windows Presentation Foundation, I came across the UI Automation framework. I thought that it sounded interesting and decided to dive a little deeper. I discovered that the framework provides access to the UI elements on the user’s desktop, allowing for manipulation and programmatic interaction with the user interface. As I continued learning more about the UI Automation framework, I began to understand how useful it would be in creating automated unit tests against the user interface.
This article aims to provide an introduction to the UI Automation framework and how it can be used to create automated unit tests against the user interface. Although the framework can also be used to manipulate Win32 and Windows Forms applications, I will be testing two sample WPF applications. These unit tests are written using Microsoft’s Unit Testing Framework, but the concepts presented are applicable to tests written using other testing frameworks, including NUnit, Rhino Mocks, and NMock.