Video in progress

Staff 🧪 Testing

Should you set a mandatory coverage threshold (say, 80%) for the whole organization?

I'm against a global hard number: it pushes people to write tests with no asserts and getter tests. Instead I choose a "coverage must not drop for changed code" rule, a higher threshold for critical modules, and mutation score.

📖 Answer

Goodhart's law: when a measure becomes a target, it stops being a good measure. Where 80% was mandatory, what I've seen is: tests of DTO getters, toString tests, tests that swallow exceptions, "smoke" calls with no asserts. The number goes up, the quality doesn't.

At the same time, ignoring coverage completely is a mistake too. Low coverage is a clear signal: in a module at 30%, something isn't tested. High coverage guarantees nothing. So it's an asymmetric indicator.

Rules that work in practice:

  • Diff coverage. For lines added/changed in a PR, for example 70%+. It doesn't "punish" legacy code and builds the right habit for new code. Tools that compare the JaCoCo report with the diff (for example, SonarQube's "new code" concept) do this.
  • Ratchet. The module's coverage must not drop; it doesn't have to rise.
  • Tiering by risk. Billing, payouts, permissions get a high threshold and mutation testing. DTOs for the admin UI get no threshold.
  • An exclusion list. Generated code, configuration classes, main are excluded from the calculation, otherwise the number is meaningless.

If leadership asks for a single number, I give them "incidents caused by regressions" and "change failure rate" instead of coverage. They show the real outcome of the tests.

The subtle point in an interview: saying "coverage is useless" and saying "80% is required" are both superficial. A strong answer says which decision it helps you make.