A pre-commit hook that enforces a coverage quality gate using coverage.xml and a baseline file
Project description
pytest-coverage-gate
A pre-commit hook that enforces a coverage quality gate for Python projects.
It reads the line-rate from coverage.xml (generated by pytest-cov) and compares it against a baseline stored in .coverage-baseline. If coverage regresses the commit is blocked. When coverage improves, the baseline is updated automatically.
How it works
| Situation | Exit code | Message |
|---|---|---|
coverage.xml or .coverage-baseline missing |
1 |
[coverage-gate] ERROR: … |
| Current coverage < baseline | 1 |
[coverage-gate] FAIL: … |
| Current coverage == baseline | 0 |
[coverage-gate] OK: … |
| Current coverage > baseline | 0 |
[coverage-gate] SUCC: … — baseline file updated |
Requirements
- Python ≥ 3.9
- A
coverage.xmlfile produced bypytest --cov --cov-report=xml - A
.coverage-baselinefile containing a single floating-point number (e.g.85.00)
Setup
1. Generate coverage.xml
Add a step to your test run (locally or in CI) that produces coverage.xml:
pytest --cov=<your_package> --cov-report=xml
2. Create .coverage-baseline
Set your initial baseline (you can use the current coverage percentage):
echo "0.00" > .coverage-baseline
git add .coverage-baseline
Tip: commit
.coverage-baselineto version control so the gate is enforced for every contributor.
3. Add the hook to .pre-commit-config.yaml
repos:
- repo: https://github.com/kelsoncm/pytest-coverage-gate
rev: v1.0.1 # use the latest tag
hooks:
- id: pytest-coverage-gate
If coverage.xml or .coverage-baseline are not in the repository root (e.g. they live under src/), pass custom paths via args:
repos:
- repo: https://github.com/kelsoncm/pytest-coverage-gate
rev: v1.0.1
hooks:
- id: pytest-coverage-gate
args:
- --coverage-xml=src/coverage.xml
- --baseline=src/.coverage-baseline
4. Run pre-commit
pre-commit run pytest-coverage-gate --all-files
Or it runs automatically on every git commit once pre-commit is installed:
pre-commit install
Standalone usage
The script can also be run directly without pre-commit:
pip install pytest-coverage-gate
pytest-coverage-gate
Use --coverage-xml and --baseline to point to files in non-default locations:
pytest-coverage-gate --coverage-xml src/coverage.xml --baseline src/.coverage-baseline
Example output
[coverage-gate] SUCC: cobertura evoluiu de atual 82.00% para 85.50%
[coverage-gate] FAIL: cobertura regrediu de atual 85.50% para 79.00%
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_coverage_gate-1.0.0.tar.gz.
File metadata
- Download URL: pytest_coverage_gate-1.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c9f52272257e1502ce96774882371bf40b7e28f1bd389cb91b2be51d2cd6edb
|
|
| MD5 |
604b1fc4cb6a3edc4e7782a4625ab699
|
|
| BLAKE2b-256 |
217c9c03c83dca0d90d2b6449b1ddb308d1bce5a7faa702d998c1a0d39997563
|
Provenance
The following attestation bundles were made for pytest_coverage_gate-1.0.0.tar.gz:
Publisher:
publish.yml on kelsoncm/pytest-coverage-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_coverage_gate-1.0.0.tar.gz -
Subject digest:
1c9f52272257e1502ce96774882371bf40b7e28f1bd389cb91b2be51d2cd6edb - Sigstore transparency entry: 1290691992
- Sigstore integration time:
-
Permalink:
kelsoncm/pytest-coverage-gate@d306483d7691272fac288c103574825e2ef305a7 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/kelsoncm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d306483d7691272fac288c103574825e2ef305a7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_coverage_gate-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_coverage_gate-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
236bb50ee037c882b64b1ff229f1160941a8fa810279475f2cc1ddbeaacf1657
|
|
| MD5 |
a31745ca4e15dbde28a960ac40fe6bde
|
|
| BLAKE2b-256 |
0c73b7a94bc51b1fc9e5426ab1c54b97a625a8ca4fc079766d5698ac2018ce05
|
Provenance
The following attestation bundles were made for pytest_coverage_gate-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on kelsoncm/pytest-coverage-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_coverage_gate-1.0.0-py3-none-any.whl -
Subject digest:
236bb50ee037c882b64b1ff229f1160941a8fa810279475f2cc1ddbeaacf1657 - Sigstore transparency entry: 1290692045
- Sigstore integration time:
-
Permalink:
kelsoncm/pytest-coverage-gate@d306483d7691272fac288c103574825e2ef305a7 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/kelsoncm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d306483d7691272fac288c103574825e2ef305a7 -
Trigger Event:
push
-
Statement type: