Your best bet is to make tests that are idempotent. We use a tool called nosedjangonosedjango to run all of our tests that handles the database, cache, etcs. This would only work if you are using django however. I am sure there are equivalent tools for whatever it is you might be using.