Properties of good unit tests
I’ve said calculating code coverage is overrated. But I’m also a strong believer in good testing. So what makes for good tests?
This is a nonexhaustive list of some charactaristics I look for when writing tests, or reviewing tests others have written. What would you add?
- Independent. A test should work by itself, or when executed with others. Tests that depend on state configured by previous tests are broken.
- Deterministic. Obviously a test that has a non-deterministic result is not a good test.
- Reliable. A test that randomly fails is potentially worse than no test at all.
- Easy to read. I place a high value on readable code. Tests are code, and thus should also be readable.
- Isolated. In a perfect world, each regression will cause one, and only one test to fail. This isn’t always practical, but it’s a good goal to aim for.
- Meaningful. This one is a bit subjective, but as an example: Testing a simple getter or setter may not be meaningful. Testing the standard library probably isn’t meaningful.