Skip to main content

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

Installation via PyPI

PyPI version

pip install pytest-coverage-gate

Requirements

  • Python ≥ 3.9
  • A coverage.xml file produced by pytest --cov --cov-report=xml
  • A .coverage-baseline file 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-baseline to 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

Metadata

Release files for pytest-coverage-gate 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-coverage-gate 1.0.3
File Size Uploaded
pytest_coverage_gate-1.0.3.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-coverage-gate 1.0.3
File Interpreter ABI Platform
pytest_coverage_gate-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / pytest_coverage_gate-1.0.3.tar.gz

Download URL pytest_coverage_gate-1.0.3.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4a0f3dfae95d2d50a7f6bfd5b806f1f42fe392fe57c96041fee850658d6b00a6
BLAKE2b-256 checksum
How to use checksums
a44779a35bfc134a73c776fe2f6cc278fa9464a2eb403ac48f9e09e477529356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 14, 2026.

Transparency log

Release files / pytest_coverage_gate-1.0.3-py3-none-any.whl

Download URL pytest_coverage_gate-1.0.3-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71769f661980541efd52cd498a95711ff94cee15667efe46451a749a1c36698a
BLAKE2b-256 checksum
How to use checksums
30106e618b3f3032b381282c02642023f48fd3a7b5fa0cb68bfde902af1294f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page