A small unit test primer (part two)

For some reason on the drive home yesterday, I realised there were a few more important points about unit tests (examples) worth mentioning.

  • They should be fast. I'm talking Civic-Hatchback-with-a-Type-R-sticker-on-the-bumper fast.
  • They should be independent. There is no guarantee that the tests will run in the same order each time, and that's OK. It also leads directly to the next point.
  • They should not alter the machine state. After a test is complete the system should be in the same state as it was before the run.
  • They should not depend on external services (where possible). I'm talking about web servers, network connections, Bob's Hooters & Google Maps mashup web service, etc.
  • They should be consistent. Either they always pass, or they always fail, and neither the time of day nor position of the stars in the sky should have any effect on them whatsoever.

OK, enough soapboxing for one day.