Sub topic 8
Failing the job below a coverage threshold
This chapter assumes you can read a GitHub Actions workflow, run pytest from the command line, and have already generated a coverage report as covered in the previous chapters. Here you will make the job fail when coverage drops too low.
Why enforce a threshold
A coverage report only informs people. Nobody has to read it, so coverage can slip a little with each pull request. A threshold turns the number into a rule. If total coverage is below the limit, the step exits with a non-zero status. GitHub Actions marks that step as failed, and the job fails with it.
Two ways to set the limit
Both tools below compare total coverage to a minimum percentage. Both exit non-zero when the total is under it.
pytest --cov=src --cov-fail-under=80
# or, after tests have already run:
coverage report --fail-under=80--cov-fail-underbelongs to pytest-cov. It runs in the same step as the tests, so one command both tests and checks the limit.coverage report --fail-underbelongs to coverage.py. It reads the data file left by an earlier run, so you can run it as a separate step.
The separate step is often clearer. A failure in the "Check coverage" step tells the reader the tests passed but coverage was too low. A single combined step makes both causes look the same.
Placing the check in a workflow
Put the threshold check after the reports are written and uploaded. Otherwise a coverage failure can stop the job before the evidence is saved.
- run: pytest --junitxml=reports/junit.xml --cov=src --cov-report=xml:reports/coverage.xml
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-reports
path: reports/
- name: Enforce coverage threshold
run: coverage report --fail-under=80Here pytest only collects data and writes reports. It does not enforce a limit. The upload step runs even if tests failed, and the final step decides whether coverage is acceptable.
Common errors
- No data to check. If the tests step failed before writing coverage data,
coverage reporthas nothing to read and fails with a different message. Read the earlier step before blaming the threshold. - Wrong measurement scope. The percentage depends on what
--cov=srcincludes. If you point it at a smaller package, the number rises without any test improvement. - Branch coverage mixed in. With branch coverage on (
--cov-branch), the total counts branches as well as lines. Switching it on usually lowers the total, so a threshold that passed before may fail. - Matrix jobs. Each matrix job measures only its own run. Apply the threshold where the data is complete, not in every job.
- Unreachable limits. A threshold of 100 makes every new line of untestable code a failure. Start near your current number and raise it gradually.
Setting the number
Run the report once and read the current total. Set the threshold slightly below it, for example a current 84 percent becomes --fail-under=82. This catches real regressions without failing on small, harmless changes.
Exercise
Your workflow runs pytest --cov=src --cov-fail-under=90 and then an upload step with no if condition. Coverage is 85 percent. Will the reports be uploaded, and how would you change the workflow so they are?
Show answer
No. The pytest step exits non-zero, so the step fails and later steps without a status condition are skipped. Either move the threshold into its own step after the upload (coverage report --fail-under=90), or add if: ${{ !cancelled() }} to the upload step so it runs after the failure.
Exercise
You turn on --cov-branch and a job that passed at --fail-under=80 now fails, though no code changed. Why?
Show answer
Branch coverage counts untaken branches in addition to lines, so the total percentage is usually lower than line coverage alone. Either lower the threshold to match the new measurement or add tests for the missing branches.