What Code Coverage Measures
You'll learn: What code coverage actually tells you—and what it doesn't—about your tests.
The Core Idea
Code coverage is a metric that shows which lines, branches, or paths in your code were executed when your tests ran. Think of it like a highlighter marking every line of code that got touched during testing.
Here's the crucial distinction: coverage tells you what code ran, not whether your tests are any good.
What Coverage Tracks
Coverage tools typically measure:
- Line coverage: Which lines of code executed
- Branch coverage: Which conditional paths (
if/else) were taken - Function coverage: Which functions were called
- Statement coverage: Which statements ran
What Coverage Doesn't Tell You
Imagine you have a function that adds two numbers. You write a test that calls it with add(2, 3) but never checks the result—you just call it and move on.
Coverage would show 100% for that function, because every line executed. But your test verified nothing. It could return 5, 42, or crash, and coverage wouldn't care.
Coverage also doesn't reveal:
- Missing test cases (untested scenarios in covered code)
- Whether assertions are meaningful
- Whether edge cases are handled
- Test quality or thoroughness
Why This Matters
Coverage is useful for finding untested code—areas you haven't even run yet. It's great for spotting gaps. But high coverage doesn't guarantee good tests, and low coverage doesn't always mean poor testing.
Use coverage as a starting point for questions, not a quality score.
Key Takeaway: Code coverage measures which code executed during tests, not whether those tests are effective, complete, or verify correct behavior.