views:

104

answers:

2

Having just set up a test framework for a new web application, I realized I missed one of the big questions: "How do I make tests independent from each other?"

Years ago I have set up some complicated Ant scripting to do full cycles of deleting all database tables, creating the schema again, adding test data, starting the application, running one test and then stopping the application. That was a pain to maintain and restricted us to nightly tests due to the time it took to run the full suite. It was still worth it, but I wonder if there is an easier way.

Are there alternatives to this approach? The main criterion is that each test should not be affected by any other test in the suite, no matter if it failed or succeeded.

A: 

Sure...look into continuous integration testing using CruiseControl. You can use that along with NAnt and NUnit to run your tests, tear down and set up your environment, and a whole slew of other things. And this can be run every time someone checks in their code to your code repository. It is the only way to do code!

Andrew Siemer
I'm already using Hudson, Maven, JWebUnit and other tools. The end-to-end tests are triggered by every successful build of the main project. But the interesting question is how to set up and tear down my environment if it is actually hosted in the database as well as cached in the web application while I run my tests in a separate process.
Peter Becker
A: 

For the record: what I am doing now is to have a special resource configured in the application, which resets the whole database (deletes all content, adds default user). This resource is bound to a URL only if the application is started in "test mode". Since our application is (mostly) RESTful, adding new objects can be done from outside anyway. The base class of our tests calls the resource during set up of a test case.

I don't really like the solution since it requires changes in the application under test and is a potential security risk. In fact I test for the flag at least three times before anything gets deleted and if the flag is set there is a big, red, flashing heading right on the front page (good excuse to use the blink tag for the first time in a decade). But still it's a bit scary.

Peter Becker