DEV Community

loading...

Properties of good unit tests

tinydevops profile image Jonathan Hall Originally published at jhall.io on ・1 min read

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?

  1. Independent. A test should work by itself, or when executed with others. Tests that depend on state configured by previous tests are broken.
  2. Deterministic. Obviously a test that has a non-deterministic result is not a good test.
  3. Reliable. A test that randomly fails is potentially worse than no test at all.
  4. Easy to read. I place a high value on readable code. Tests are code, and thus should also be readable.
  5. 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.
  6. 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.

Discussion (0)

pic
Editor guide