Testing · lesson 9 of 10
Ideas in this lesson
ALine and branch coveragewhich lines ran, and which way each if went
BRegression testa test that keeps a fixed bug fixed
CFlaky testpasses or fails with no change to the code

Coverage and quality

Last updated

How do you know which parts of Pila your tests never touch? A coverage tool watches the tests run and reports every line and branch they didn't reach. It's a useful map of what's untested, and a poor score of how good the tests are. In this lesson you'll measure Pila's coverage in the page, close the gaps, and learn what a high number does and doesn't prove, along with two habits that keep a suite trustworthy: a test for every fixed bug, and no flaky tests.

By the end of this lesson you can

  • Run coverage and read which lines and branches are untested
  • Explain what 100% coverage does and doesn’t mean
  • Add a regression test for every fixed bug, and deal with flaky tests

What coverage measures

A coverage tool watches the code while the tests run, and records what ran. Python's is Coverage.py. It measures two things:

Two kinds of coverage
KindAsksMisses
Line (statement) coverageDid each line run at least once?An if whose condition was always true: its line ran, but the “false” path never did
Branch coverageDid each if go both ways?Much less, which is why teams usually turn it on

The report lists, for each file, how many statements and branches there are, how many were missed, a percentage, and the missing line numbers: the map of what no test has touched.

Measure Pila’s tests

This program runs the tests in test_queue.py with coverage watching queue_rules.py, then prints the report. Run it and read the Missing column: those line numbers are in the queue_rules.py tab. Then add tests in the test_queue.py tab until every line and branch is covered.

Step 2 code
Edit the code, then run it again

The two missing lines were the two ways Pila says no: outside opening hours, and when the day is full. Those are exactly the cases that matter most to a patient, and the original tests never tried them. On a computer, the same report comes from python -m pytest --cov with the pytest-cov plugin, or coverage run -m pytest then coverage report -m.

What 100% doesn’t mean

Coverage shows what ran, not what was checked. A test with no assert (lesson 6.2's trap) still runs every line it calls. Delete every assert from your tests above and run again: coverage stays at 100%, and the suite would now pass with any bug at all.

Coverage also can't see what's missing from the code. If Pila never handled a phone number with spaces in odd places, there's no line for coverage to report as untested.

Martin Fowler's advice: coverage is useful for finding untested code, and of little use as a target. A low number is a warning worth reading. A high number, on its own, proves little. Many healthy teams sit somewhere around 80–90% and look at what's missing, rather than chasing a figure.

Regression tests

A regression is a bug that was fixed and comes back, or something that used to work and stops. They're common: a later change undoes a fix nobody remembered was delicate.

The habit that stops them: every fixed bug gets a test. When Marco fixes lesson 6.7's duplicate-number bug, the pull request starts with a test that reproduces it, straight from Rina's steps (lesson 6.8): give out numbers, cancel one, let a new phone text, and assert the numbers are all different. The test fails before the fix and passes after. From then on, it fails if anyone ever brings the bug back.

Flaky tests

A flaky test sometimes passes and sometimes fails, with no change to the code. Martin Fowler calls them dangerous out of all proportion: once a team learns that a red build is “probably just that flaky test”, they start ignoring red builds, and a real failure slips through.

Common causes, and Pila's examples:

  • Time. A test that calls the real clock passes before 17:00 and fails after. Pila's tests pass the time in, as time(8, 0), so they give the same answer at any hour.
  • Order. A test that relies on another test running first, or leaves data behind for the next one. Each Pila test makes its own Queue().
  • The outside world. A network, a real gateway, a shared database (lesson 6.4's fakes fix this).

When a test turns flaky, fix it or take it out of the suite at once, with a card on the board to bring it back. Don't leave it to cry wolf.

Spot the trap: chasing 100%

Spot the trapKap. Ernesto

Kap. Ernesto reads that good teams have high coverage. He asks for 100% by Friday, and says he’ll check the number every week.

What happens if…

Check what you learned

An if’s condition was true in every test. Line coverage shows the line as covered. What does branch coverage show?
Pila’s coverage is 100%. What does that prove?
Marco fixes a bug. What should the pull request include?
A test fails only when the suite runs after 17:00. What kind of test is it?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development