Stop 10 · Testing & Code QualityTopics · 4/8

Test Coverage with coverage.py

Part of the Python Roadmap

In short

You have forty tests and they all pass. But which lines of your code did they never touch? Coverage answers that, and the untested lines are usually where the next bug is hiding.

Coverage tools measure which lines your tests actually run, showing untested code paths. pytest-cov or coverage.py produce a report, but high coverage alone doesn't prove the tests check the right things.

The jaadu shines a torch along a freshly painted lavender wall, spotting a few unpainted patches hidden behind a tall wooden cupboard.

Think of it like this

Painting the walls before Diwali

After painting the house, you walk around with a torch looking for patches you missed behind the almirah and above the door. A coverage report is that torch walk: it lights up every line your tests ran and shows the patches they never reached.

Watch it happen

Which lines of shipping_fee() did the tests run?

def fee(amt)
0
if amt <= 0
1
raise Error
2
if amt >= 499
3
return 0
4
return 40
5

1/4Six lines in the function. We have one test: fee(200) should be 40.

The big ideas

What you need to know

Line coverage

The share of lines executed at least once during the tests. A quick health check, not a quality score.

Branch coverage

Checks that both the true and false side of each if ran. Turn it on with --cov-branch.

term-missing

The most useful report: it lists the exact line numbers no test reached, right in your terminal.

100% isn't proof

A test that runs a line without asserting anything still counts. Coverage shows what's untested, not what's tested well.

See it in code

Walk through the code

bash
1pip install pytest-cov2 3# Run tests and measure the kirana_billing package4pytest --cov=kirana_billing --cov-branch --cov-report=term-missing5 6# Browse a coloured line-by-line report7pytest --cov=kirana_billing --cov-report=html8# then open htmlcov/index.html9 10# Fail the run if coverage drops below 80%11pytest --cov=kirana_billing --cov-fail-under=8012 13# Same thing with plain coverage.py14coverage run -m pytest15coverage report -m

1/4--cov names the package to measure. term-missing adds a Missing column listing untested lines.

pytest-cov is a pytest plugin built on coverage.py. Both give the same numbers.

Save yourself the pain

Common mistakes

Oops

Chasing 100% by writing tests with no assertions.

Do this instead

Coverage should guide you to untested behaviour. Every test still needs a real assert.

Oops

Measuring with --cov=. and counting your test files as covered code.

Do this instead

Point --cov at your package (for example --cov=kirana_billing) so the number reflects your real code.

Oops

Ignoring branch coverage.

Do this instead

Line coverage can be 100% while the else path of an if never runs. Add --cov-branch.

Your mission

Find the dark corners

~25 min

Quick check

Your coverage report shows 100%. What can you safely conclude?

Go deeper

  1. 1Run pytest --cov and open the HTML report.
  2. 2Write tests for the branches the report shows as missed.
  3. 3Aim for meaningful tests on important logic rather than chasing 100 percent.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge