HTML and CSS Reference
In-Depth Information
well-formed pages and different things in malformed ones. Thus, one benefit of refactoring is improving
testability and making test-driven development possible in the first place. Indeed, with a lot of web sites that
don't already have tests, you may need to refactor them enough to make testing possible before moving
forward.
You can use many tools to test web pages, ranging from decent to horrible and free to very expensive. Some of
these are designed for programmers, some for web developers, and some for business domain experts. They
include
HtmlUnit
JsUnit
HttpUnit
JWebUnit
FitNesse
Selenium
In practice, the rough edges on these tools make it very helpful to have an experienced agile programmer
develop the first few tests and the test framework. Once you have an automated test suite in place, it is usually
easier to add more tests yourself.
JUnit
JUnit ( www.junit.org/ ) is the standard Java framework for unit testing and the one on which a lot of the more
specific frameworks such as HtmlUnit and HttpUnit are built. There's no reason you can't use it to test web
applications, provided you can write Java code that pretends to be a browser. That's actually not as hard as it
sounds.
For example, one of the most basic tests you'll want to run is one that tests whether each page on your site is
well-formed. You can test this simply by parsing the page with an XML parser and seeing whether it throws any
exceptions. Write one method to test each page on the site, and you have a very nice automated test suite for
what we checked by hand in the previous section.
Listing 2.2 demonstrates a simple JUnit test that checks the well-formedness of my blog. All this code does is
throw a URL at an XML parser and see whether it chokes. If it doesn't, the test passes. This version requires
Sun's JDK 1.5 or later, and JUnit 3.8 or later somewhere in the classpath. You may need to make some
modifications to run this in other environments.
Listing 2.2. A JUnit Test for Web Site Well-Formedness
Search WWH ::




Custom Search