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 Docs

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/python-by-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/python-by-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

GitHub Action

Use the gate as a step in your workflow. It runs from the action's own checkout (standard library only, no pip install), so pinning @v1 pins the gate version too.

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: "3.x"
      - run: pip install pytest pytest-cov
      - run: pytest --cov=<your_package> --cov-report=xml

      - name: Coverage gate
        id: gate
        uses: python-by-kelsoncm/pytest-coverage-gate@v1
        # with:
        #   coverage-xml: src/coverage.xml
        #   baseline: src/.coverage-baseline

The step fails when coverage drops below the committed baseline and writes a summary table to the job page.

Input Default Description
coverage-xml coverage.xml Path to the Cobertura report
baseline .coverage-baseline File holding the baseline percentage
update-baseline false When true, rewrites the baseline in the workspace if coverage improved
python-version 3.x Python used to run the gate (3.9+)
Output Description
current Current coverage percentage
baseline Baseline percentage
delta current - baseline, in percentage points (e.g. +1.25)
status ok, improved or fail

CI never rewrites the baseline by default (update-baseline: false): a change made in the runner is lost when the job ends. To raise the baseline, run the hook/CLI locally (it updates .coverage-baseline for you) and commit the result, or set update-baseline: true and commit/open a PR from a later step.

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: coverage improved from 82.00% to 85.50%
[coverage-gate] FAIL: coverage regressed from 85.50% to 79.00%

Pass --check-only to never rewrite the baseline (used by the GitHub Action by default).

License

MIT

Metadata

Release files for pytest-coverage-gate 1.1.0

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.1.0
File Size Uploaded Provenance Actions
pytest_coverage_gate-1.1.0.tar.gz 14.6 kB With provenance Details for pytest_coverage_gate-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 21.6 kB

Release files / pytest_coverage_gate-1.1.0.tar.gz

Download URL pytest_coverage_gate-1.1.0.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
db34dd87f041c6addb1fcce7c029fa27b7948e877297daeaeac34c8cbb51c17f
BLAKE2b-256 checksum
How to use checksums
721b57b9201d10e621f10ed26dbaa807c0929619c40ff8773a9e6fff9e55c20d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 27, 2026.

Transparency log

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

Download URL pytest_coverage_gate-1.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
231c73114341576b9d97a8d6f33d20ef0aec1a55fbdb76f07aae410269e9ea2b
BLAKE2b-256 checksum
How to use checksums
0c4426d6c284db329347cbad3a8e2ac0b88a42b031c6d8e8a7de5066493dbd27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 27, 2026.

Transparency log

Security for pytest-coverage-gate 1.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_coverage_gate-1.1.0.tar.gz

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 Oct 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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