Testing Web Applications

Yesterday I had a brief chat with a student doing 4th year software engineering at my university. He had just given a presentation on the testing methods his team was using for their web application and he said something along the lines of "we're doing lots of manual testing because there's no way to test web software". Not so! Here's a short overview of some of the software and methods you can use to increase the amount of automated testing you're doing on your web applications.

Server-side Testing

I would assume that most people write the server side of their web applications in PHP, Python, Ruby or Java. Each of these languages has at least one application or framework for unit testing, usually based on the xUnit style of unit testing. You have PhpUnit, PyUnit and, of course, JUnit. On top of these you can often find other testing software or software to aid you in building test suites and the like.

Really, server-side testing is no different to "regular" automated testing, even conceptually, so I don't think many people would have much problem with it. You have some code on a computer that gets executed and returns results. Testing that is just a matter of writing up the proper tests and executing them. Assuming you have terminal access to the computer running the server code, this process is trivial. Whether you're using Ruby, PHP, Perl, C or assembler this doesn't change.

The only thing you might run into trouble with is testing things which are meant to have a connection to a database. In this case you would use mock objects to simulate the behaviour of a proper database. Similarly you could use mocks to test various types of requests from user agents on the client side.

Browser-side Testing

I think that the client side of things is where most people get tripped up. Students who have never done much testing often just assume that once the server has sent off the HTML, JavaScript and CSS then those things cannot be tested because they're being sent to a web browser, which isn't exactly the best tool for testing software. Obviously, this isn't right.

First of all, a web browser is simply a type of client software (ie. a type of user agent) that is designed to take output from the server and construct a visual rendering of that output for a user. You have to remember that all the web browser is being sent is HTTP responses and semi-structured plain text. Really – that's it. How can you not be able to test plain text? (For the purposes of this discussion I'm assuming that you're able to check whether a user agent is requesting and receiving the correct binary files as well and don't need to test, say, if the awesome.jpg file really is a picture of a Bugatti Veyron.)

So what tools do we have to test server output? If you want to be really old school (and you're using a Unix-like OS … which I guess amounts to the same thing) then you can use text dumping in lynx and use diff to compare it to your expected HTML. You could also use wget or something similar to retrieve the page from the server.

For something more advanced, you can use HttpUnit or HtmlUnit to emulate a browser. There's also mechanize if you prefer using Python and I'm sure you can find others for your language of choice, or be able to whip up one of your own fairly quickly. (For large teams this might give other team members something to do!)

This will test whether you are getting the correct responses, and you can automate comparisons to expected HTML output but what about the logic of your JavaScript? Well, you're covered there too. As you might have already guessed, there is an xUnit-based unit testing framework for JavaScript called JsUnit. If you just want to debug your JavaScript (after testing it) then you could use Firebug, which is a Firefox extension but also available in limited fashion for other browsers.

Since you will likely still need – and want – to do manual testing of your application in a web browser (possibly as part of usability testing) then you can use Selenium to help you bring some consistency to the process and automate some of it. With it you can create some tests that can be run inside of a web browser. You can use any web browser that is supported by Selenium (the majority of them) so you can check for things that other types of tests would miss, such as whether your application actually works on different browsers!

Conclusion

I did not intend this post to be a complete listing of all the software or methods available for testing in the web application domain but hopefully you'll now have a better understanding of what's out there and how you can apply it to an overall testing strategy. I believe that the more testing that can be automated the better. Remember that engineers are inherently lazy and abhor doing menial, repetitive tasks so if the tests aren't automated they won't get done!