You could easily find yourself mired in programmer debates over code coverage. Here’s one, for instance. It raged on for hundreds of votes, dozens of comments, many answers, and eight years before someone put it out to pasture as “opinion-based” and closed it.
Discussion participants debated a “reasonable” percentage of code coverage. I imagine you can arrive at the right answer by taking the mean of all of the responses. (I’m just kidding.)
Some of the folks in that thread, however, declined to give a simple percentage. Instead, they gave the obligatory consultant’s response of “it depends” or, in the case of the accepted answer, a parable. I found it refreshing that not everybody picked some number between 50 and 100 and blurted it out.
But I found it interesting that certain questions went unasked. What is the goal of this code coverage? What problem does it solve? And as for the reasonability of the number, reasonable to whom?
What is Code Coverage? (Briefly)
Before going any further, I’ll quickly explain the concept of code coverage for those not familiar, without belaboring the point. Code coverage measures the percentage of code that your automated unit test suite executes when it runs.
So let’s say that you had a tiny codebase consisting of two methods with one line of code each. If your unit test suite executed one method but not the other, you would have 50% code coverage. In the real world, this becomes significantly more complicated. In order to achieve total coverage, for instance, you must traverse all paths through control flow statements, such as if conditions, switch statements, and loops.
When people debate how much code coverage to have, they aren’t debating how “correct” the code should be. Nor do they debate anything about the tests themselves, such as quantity or quality. They’re simply asking how many lines of code per one hundred your test suite should cause to execute.
You can get tools that calculate this for you. And you can also get tools that factor it into an overall code quality dashboard.