Monday, February 8, 2010

Properties of Good Tests:

Independent - No test should affect the outcome of any other test. Put another way, you should be able to run your tests in any order and always have the same outcome. A corollary of this is that setup/teardown methods are evil (both because they increase dependence and they decrease readability)
Readable - The intent of each test should be immediately obvious (both by its name and by its code).
Fast - Each test should run as quickly as possible, so the entire suite is also fast. The faster the suite, the more you'll run the tests, and the greater benefit you'll get (because you'll catch regressions quickly)
Precise - Each test should focus on testing one thing (and only one thing) well. Ideally, if a test fails, you should know exactly what part of your production code broke by just glancing at the name of the test. Also, if your tests are precise, it's less likely that a change in your code will require you to change many different tests. In practice, precise tests are short and only have one assertion or expectation per test.

No comments: