Statement Coverage
What is Statement Coverage?
Statement coverage is a white box testing technique that measures the percentage of executable statements in the source code that have been executed by the test suite.
How It's Calculated
Statement coverage = (Number of statements executed / Total number of statements) × 100
Limitations
While statement coverage ensures every line has run at least once, it does not guarantee that all logical branches (true/false conditions) have been tested — for that, branch coverage is needed as well.
Worked Example: Counting Statements by Hand
Look at this small Python function that turns an exam score into a label. Each numbered line is one executable statement.
def get_label(score):
label = None # S1
if score >= 50: # S2
label = "pass" # S3
return label.upper() # S4
There are four executable statements. Now run a single test with score = 70. Line S1 runs, the condition on S2 is checked and is true, S3 assigns "pass", and S4 returns "PASS". All four statements ran, so:
Statement coverage = 4 / 4 × 100 = 100%
The coverage report looks perfect, yet the function has a bug. Call it with score = 30 and S3 is skipped, so label stays None and S4 crashes with an AttributeError. One test touched every line but never tried the false side of the condition. This is the clearest demonstration of why statement coverage alone is a weak target.
Partial coverage example
If instead the only test used score = 30, statements S1, S2 and S4 run but S3 does not. Coverage would be 3 / 4 × 100 = 75%, and that test would actually reveal the crash. A lower number found the bug while the higher number hid it, so read coverage as a map of untested code, not as a quality score.
Measuring It With Real Tools
You rarely count statements by hand in real projects. Coverage tools record which lines run while your tests execute and then print a report.
pip install coverage pytest
coverage run -m pytest
coverage report -m # -m lists the line numbers that never ran
coverage html # writes an htmlcov folder you can open in a browser
Other languages have similar tools: JaCoCo for Java, Istanbul (through nyc or built into Jest) for JavaScript, and gcov for C and C++. Most of them can report branch coverage as well; turn that option on.
Using Statement Coverage Sensibly
- Use it to find dead spots. Lines that never run in any test are the most valuable part of the report.
- Pair it with branch coverage. Branch coverage would have flagged the missing false path in the example above.
- Do not chase 100% blindly. Tests written only to touch lines, without real assertions, raise the number but catch nothing.
- Know what counts. Comments, blank lines and declarations are not executable, so tools ignore them. Different tools may count slightly differently.
- Check assertions. Coverage tells you code ran, not that its result was checked.
Takeaway: treat statement coverage as the minimum bar. If a line has never run in a test, you know nothing about it. Once every line has run, move on to branches and meaningful input values.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.