pytest-coverage-gate
=====================
.. image:: https://img.shields.io/badge/GitHub-Repository-blue?logo=github
:target: https://github.com/python-by-kelsoncm/pytest-coverage-gate
:alt: GitHub Repository
.. image:: https://img.shields.io/badge/License-MIT-lemon.svg
:target: https://opensource.org/licenses/MIT
:alt: License
.. image:: https://img.shields.io/pypi/v/pytest-coverage-gate.svg
:target: https://pypi.org/project/pytest-coverage-gate/
:alt: PyPI version
.. image:: https://github.com/python-by-kelsoncm/pytest-coverage-gate/actions/workflows/publish.yml/badge.svg
:target: https://github.com/python-by-kelsoncm/pytest-coverage-gate/actions/workflows/publish.yml
:alt: Publish
.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit
:target: https://github.com/pre-commit/pre-commit
:alt: pre-commit
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
-------------
.. list-table::
:widths: 40 15 45
:header-rows: 1
* - 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
----------------------
.. code-block:: bash
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``:
.. code-block:: bash
pytest --cov= --cov-report=xml
2. Create ``.coverage-baseline``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set your initial baseline (you can use the current coverage percentage):
.. code-block:: bash
echo "0.00" > .coverage-baseline
git add .coverage-baseline
.. note::
Commit ``.coverage-baseline`` to version control so the gate is enforced for every contributor.
3. Add the hook to ``.pre-commit-config.yaml``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: 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``:
.. code-block:: yaml
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
~~~~~~~~~~~~~~~~~~
.. code-block:: bash
pre-commit run pytest-coverage-gate --all-files
Or it runs automatically on every ``git commit`` once pre-commit is installed:
.. code-block:: bash
pre-commit install
Standalone usage
-----------------
The script can also be run directly without pre-commit:
.. code-block:: bash
pip install pytest-coverage-gate
pytest-coverage-gate
Use ``--coverage-xml`` and ``--baseline`` to point to files in non-default locations:
.. code-block:: bash
pytest-coverage-gate --coverage-xml src/coverage.xml --baseline src/.coverage-baseline
Example output
---------------
.. code-block:: text
[coverage-gate] SUCC: cobertura evoluiu de atual 82.00% para 85.50%
.. code-block:: text
[coverage-gate] FAIL: cobertura regrediu de atual 85.50% para 79.00%
API Reference
--------------
.. automodule:: coverage_gate
:members:
:undoc-members:
:show-inheritance: