Based on the interesting feedback I got (can be seen on Tom’s ramblings), I realized this post probably needed some tweaking and scope precision. I did put them at the end.
What is the adequate objective for test coverage?
Like many others, I have often pondered this question, like many before me, and many after I suppose. Why aiming for 100%? The 80/20 law clearly applies to test coverage: to try to cover every corner cases that lie in the code is going to require a significant investment in time and brain cells. Plus integration point can never really be properly covered.
On the other hand, having 100% coverage provides huge benefits:
- Every single line of code is constantly tested
- Trust in code is high
- Any uncovered line is a regression
What happens if the target is, 80%:
- Significant part of the code is never tested
- Trust in code is moderate and can degrade
- 20% uncovered line codes is significant, 2000 lines for a 10K code base. That means full namespaces can hide in there.
For me, there is no question 100% coverage is the only worthy objective, do not settle for less.
Yes there are exceptions, usually at integration points. Mocks are not a real solution either, they can help you increase your coverage but not by that much. The pragmatic solution is to wrap them into isolated modules (jars/assemblies). Think hexagonal architecture there. You will have specific coverage target for those, you also need to make sure that no other code creeps in and finally, understands those are weak points in your design.
While I am working on nFluent, I constantly make sure unit tests exert every single line of code of the library. It also means that I help contributors reach the target. It is not that difficult, especially if you do TDD!
There is one simple golden rule: to reach and maintain 100% coverage, you do not need to add tests, you have to remove not covered lines!
This is important, so let me restate that: a line of code that is not covered is not maintainable, must be seen as not working and must be removed!
Think about it:
- The fact that no automated test exists means that the behavior can be silently changed, not just the implementation!
- Any newcomer, including your proverbial future self, will have to guess the expected behavior !
- What will happen if the code get executed some day in production?
- If you are doing TDD you can safely assume the code is useless!
So, when you discover not covered lines, refactor to remove them or to make sure they are exerted. But do not add tests for the sake of coverage.
Having 100% coverage does not mean the code is bug free
Tom’s comments implied that I was somehow trying to promote this idea. 100% coverage is no bug free proof at all, and do not imply this at all. Quality and relevance of your tests are essential attributes; that is exactly why I promote removing non tested lines. Any specially crafted test will not be driven by an actual need and would be artificial. The net result would be a less agile code base.
On the other hand, if you have 100% coverage and you discover some reproducible bug, either by manual testing or in production, you should be able to add an automated test to prevent any re occurrence.
When coverage is insufficient, there is a high probability that you will not be able to add this test, keeping the door open for future regression!
If you want to build trust based on coverage metrics, you need to look into branch coverage and property based testing at the very least. But I do not think this is a smart objective.
- This post focuses on new code! For legacy code, the approach should be to add tests before anything else, and never remove working code 🙂