What is test coverage, and why should achieving a high test coverage percentage not necessarily be treated as the sole ultimate goal of a testing strategy?
Short answer
Test coverage measures what percentage of a codebase's actual lines or branches of code are executed at least once while running the full test suite, and while reasonably high test coverage is generally a positive sign, it should not be treated as the sole measure of genuine…
Advertisement
AModel answer
Test coverage measures what percentage of a codebase's actual lines or branches of code are executed at least once while running the full test suite, and while reasonably high test coverage is generally a positive sign, it should not be treated as the sole measure of genuine test quality, since a test can technically execute a line of code without actually verifying that the code produced the correct result, meaning a codebase could show high coverage while still containing weak, ineffective tests that would fail to actually catch a real bug if one were introduced.
Example
vendor/bin/phpunit --coverage-html coverage-report
Real-world example
A team notices their codebase shows ninety five percent test coverage but still experiences frequent production bugs, and upon investigation discovers many of their existing tests execute the relevant code without actually asserting anything meaningful about its correctness.
Likely follow-ups
- What is the difference between line coverage and branch coverage?
- What are better indicators of genuine test quality beyond a simple coverage percentage?
Related topics