What is code coverage?
Answer
Code coverage is a metric measuring what percentage of the source code is executed when the test suite runs. Types: Line/Statement coverage: percentage of lines executed. Branch coverage: percentage of branches (if/else paths) taken. Function coverage: percentage of functions called. Path coverage: percentage of unique paths through the code (can be exponential). Tools: Istanbul/nyc (JavaScript), JaCoCo (Java), coverage.py (Python), Coverlet (.NET), PHPUnit coverage. High coverage is NOT sufficient — it means lines were executed, not that the assertions were correct. 100% coverage doesn't mean bug-free. Common misconception: 80% coverage mandate — this can lead to meaningless tests written purely to hit the number. Better: focus on testing all meaningful behaviors, edge cases, and error paths. Coverage is a useful tool to find untested code (low coverage = risk), not a quality metric on its own.
Previous
What is mocking in unit testing?
Next
What is the difference between a test stub and a mock?