How to Fail CI Under a pytest Coverage Threshold
pytest --cov-fail-under=N makes the run fail when total coverage is below N percent.
Pass --cov-fail-under=<N> on the command line or set fail_under in the coverage config. Either way pytest returns a non-zero exit code when coverage is short.
Steps
- Add
--cov-fail-under=<N>to the pytest command, or setfail_underin config. - Keep
--cov=<package>so the percentage reflects your code. - A shortfall exits non-zero and fails the CI job.
Terminal
Terminal
pytest --cov=myapp --cov-report=term-missing --cov-fail-under=85pyproject.toml
pyproject.toml
[tool.coverage.report]
fail_under = 85
show_missing = trueGotchas
- The command-line
--cov-fail-underoverrides the configfail_underwhen both are present. - The threshold is checked against the combined total, so parallel runs must be merged before the check.
Related guides
How to Generate pytest Coverage in CIMeasure Python test coverage in CI with pytest-cov, running pytest --cov to print a report and emit coverage.…
How to Ratchet Coverage Upward in CIPrevent coverage from slipping in CI by using a ratchet, where the gate compares against the current level so…
How to Combine Coverage From a Matrix or Sharded Run in CIMerge coverage from parallel matrix or sharded CI jobs by uploading each shard as an artifact, then combining…