Automated unit testing is a great way to ensure that code being maintained works and continues to work. The Extreme Programming (XP) methodology relies heavily on it, and practitioners have available to them a range of unit testing frameworks, which work by making direct calls to the code being tested. But what if you want to test a web application? HttpUnit is a library which makes this possible.

HttpUnit works by emulating a browser, handling frames, cookies, redirects, and so on. It lets you view pages as plain text, an XML DOM, or as an aggregation of collection of links, frames, images, and so on. It lets you select an element which should lead to a new page and follow it, so that you can easily handle a chain of web pages.

Test Design and Abstraction

Unlike most commercial tools, HttpUnit does not rely on record and playback. Its API lets you define what you want to see or change, even before the web site is built. This is essential for test-first development. Since the primary use of HttpUnit is to write functional tests, it helps you abstract those elements which are most likely to change. For example, a page might start by providing a small number of options, using a set of radio buttons:

Select the desired color: Red Green Blue

As the site evolves, the number of options might grow until the UI designer feels that a pull-down menu would be easier to use to send the same values:

Select the desired color:

Further expansion could lead to a free-form textual input:

Enter the desired color:

A tool which focuses on the type of control actually on the page would force the testers to rewrite their tests with each change, even though the functionality of the site is unchanged. HttpUnit focuses on the intent of the entry, and allows tests to be defined in terms of the values to be transmitted. It is aware of the controls being used and ensures that only values which a user could supply will be sent; however, the tester need only focus on the desired value for each test.

Copyright © 2000-2002 Russell Gold. All rights Reserved.