top of page

Code coverage is a bad metric 👎

Updated: Aug 25



It is often seen as the gold standard for testing, but is it really? 🤷‍♂️


Unfortunately, test coverage does not reflect how well the code is tested. All test coverage does is show the amount of lines of code executed. It does not indicate that the functionality under test is actually correct.


In fact, you can achieve 100% of test coverage without actually testing anything. How? By simply not doing assertions in your tests, but still invoking the methods.


Test coverage is a good negative metric, but a bad positive one. Test coverage is useful for highlighting gaps (e.g., 0% coverage signals untested code), but it can be misleading when used to claim thorough testing.


A bit of an improvement over line coverage is branch coverage. The branch coverage report indicates the amount of conditional statements traversed. This metric is a bit harder to “trick” in comparision to line coverage. 


However, even branch coverage does not guarantee the quality of your tests. The only way to do it is to actually write the proper assertions that verify relevant outcomes.


🚨 So, don’t be fooled by high test coverage, it does not guarantee in any way that the code is safe. Write tests that verify the needed outcomes and run them often, preferrably on your CI / CD. ✅


Focus on writing meaningful tests with solid assertions. High coverage is nice, but it’s quality testing that keeps your code safe.

17 views0 comments

Kommentarer


bottom of page