Give AlbumentationsX a star on GitHub — it powers this leaderboard

Star on GitHub

pytest-cov

Pytest plugin for measuring coverage.

Downloads: 0 (30 days)

Description

========
Overview
========

.. start-badges

.. list-table::
    :stub-columns: 1

    * - docs
      - |docs|
    * - tests
      - |github-actions|
    * - package
      - |version| |conda-forge| |wheel| |supported-versions| |supported-implementations| |commits-since|

.. |docs| image:: https://readthedocs.org/projects/pytest-cov/badge/?style=flat
    :target: https://readthedocs.org/projects/pytest-cov/
    :alt: Documentation Status

.. |github-actions| image:: https://github.com/pytest-dev/pytest-cov/actions/workflows/test.yml/badge.svg
    :alt: GitHub Actions Status
    :target: https://github.com/pytest-dev/pytest-cov/actions

.. |version| image:: https://img.shields.io/pypi/v/pytest-cov.svg
    :alt: PyPI Package latest release
    :target: https://pypi.org/project/pytest-cov

.. |conda-forge| image:: https://img.shields.io/conda/vn/conda-forge/pytest-cov.svg
    :target: https://anaconda.org/conda-forge/pytest-cov
.. |wheel| image:: https://img.shields.io/pypi/wheel/pytest-cov.svg
    :alt: PyPI Wheel
    :target: https://pypi.org/project/pytest-cov

.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/pytest-cov.svg
    :alt: Supported versions
    :target: https://pypi.org/project/pytest-cov

.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/pytest-cov.svg
    :alt: Supported implementations
    :target: https://pypi.org/project/pytest-cov

.. |commits-since| image:: https://img.shields.io/github/commits-since/pytest-dev/pytest-cov/v7.0.0.svg
    :alt: Commits since latest release
    :target: https://github.com/pytest-dev/pytest-cov/compare/v7.0.0...master

.. end-badges

This plugin provides coverage functionality as a pytest plugin. Compared to just using ``coverage run`` this plugin does some extras:

* Automatic erasing and combination of .coverage files and default reporting.
* Support for detailed coverage contexts (add ``--cov-context=test`` to have the full test name including parametrization as the context).
* Xdist support: you can use all of pytest-xdist's features including remote interpreters and still get coverage.
* Consistent pytest behavior. If you run ``coverage run -m pytest`` you will have slightly different ``sys.path`` (CWD will be
  in it, unlike when running ``pytest``).

All features offered by the coverage package should work, either through pytest-cov's command line options or
through coverage's config file.

* Free software: MIT license

Installation
============

Install with pip::

    pip install pytest-cov

For distributed testing support install pytest-xdist::

    pip install pytest-xdist

Upgrading from pytest-cov 6.3
-----------------------------

`pytest-cov 6.3` and older were using a ``.pth`` file to enable coverage measurements in subprocesses. This was removed in `pytest-cov 7` - use `coverage's patch options <https://coverage.readthedocs.io/en/latest/config.html#run-patch>`_ to enable subprocess measurements.

Uninstalling
------------

Uninstall with pip::

    pip uninstall pytest-cov

Under certain scenarios a stray ``.pth`` file may be left around in site-packages.

* `pytest-cov 2.0` may leave a ``pytest-cov.pth`` if you installed without wheels
  (``easy_install``, ``setup.py install`` etc).
* `pytest-cov 1.8 or older` will leave a ``init_cov_core.pth``.

Usage
=====

::

    pytest --cov=myproj tests/

Would produce a report like::

    -------------------- coverage: ... ---------------------
    Name                 Stmts   Miss  Cover
    ----------------------------------------
    myproj/__init__          2      0   100%
    myproj/myproj          257     13    94%
    myproj/feature4286      94      7    92%
    ----------------------------------------
    TOTAL                  353     20    94%

Documentation
=============

https://pytest-cov.readthedocs.io/en/latest/


Coverage Data File
==================

The data file is erased at the beginning of testing to ensure clean data for each test run. If you
need to combine the coverage of several test runs you can use the ``--cov-append`` option to append
this coverage data to coverage data from previous test runs.

The data file is left at the end of testing so that it is possible to use normal coverage tools to
examine it.

Limitations
===========

For distributed testing the workers must have the pytest-cov package installed. This is needed since
the plugin must be registered through setuptools for pytest to start the plugin on the
worker.

Security
========

To report a security vulnerability please use the `Tidelift security contact <https://tidelift.com/security>`_.
Tidelift will coordinate the fix and disclosure.

Acknowledgements
================

Whilst this plugin has been built fresh from the ground up it has been influenced by the work done
on pytest-coverage (Ross Lawley, James Mills, Holger Krekel) and nose-cover (Jason Pellerin) which are
other coverage plugins.

Ned Batchelder for coverage and its ability to combine the coverage results of parallel runs.

Holger Krekel for pytest with its distributed testing support.

Jason Pellerin for nose.

Michael Foord for unittest2.

No doubt others have contributed to these tools as well.

Changelog
=========

7.0.0 (2025-09-09)
------------------

* Dropped support for subprocesses measurement.

  It was a feature added long time ago when coverage lacked a nice way to measure subprocesses created in tests.
  It relied on a ``.pth`` file, there was no way to opt-out and it created bad interations
  with `coverage's new patch system <https://coverage.readthedocs.io/en/latest/config.html#run-patch>`_ added
  in `7.10 <https://coverage.readthedocs.io/en/7.10.6/changes.html#version-7-10-0-2025-07-24>`_.

  To migrate to this release you might need to enable the suprocess patch, example for ``.coveragerc``:

  .. code-block:: ini

    [run]
    patch = subprocess

  This release also requires at least coverage 7.10.6.
* Switched packaging to have metadata completely in ``pyproject.toml`` and use `hatchling <https://pypi.org/project/hatchling/>`_ for
  building.
  Contributed by Ofek Lev in `#551 <https://github.com/pytest-dev/pytest-cov/pull/551>`_
  with some extras in `#716 <https://github.com/pytest-dev/pytest-cov/pull/716>`_.
* Removed some not really necessary testing deps like ``six``.

6.3.0 (2025-09-06)
------------------

* Added support for markdown reports.
  Contributed by Marcos Boger in `#712 <https://github.com/pytest-dev/pytest-cov/pull/712>`_
  and `#714 <https://github.com/pytest-dev/pytest-cov/pull/714>`_.
* Fixed some formatting issues in docs.
  Anonymous contribution in `#706 <https://github.com/pytest-dev/pytest-cov/pull/706>`_.

6.2.1 (2025-06-12)
------------------

* Added a version requirement for pytest's pluggy dependency (1.2.0, released 2023-06-21) that has the required new-style hookwrapper API.
* Removed deprecated license classifier (packaging).
* Disabled coverage warnings in two more situations where they have no value:

  * "module-not-measured" in workers
  * "already-imported" in subprocesses

6.2.0 (2025-06-11)
------------------

* The plugin now adds 3 rules in the filter warnings configuration to prevent common coverage warnings being raised as obscure errors::

    default:unclosed database in <sqlite3.Connection object at:ResourceWarning
    once::PytestCovWarning
    once::CoverageWarning

  This fixes most of the bad interactions that are occurring on pytest 8.4 with ``filterwarnings=error``.

  The plugin will check if there already matching rules for the 3 categories
  (``ResourceWarning``, ``PytestCovWarning``, ``CoverageWarning``) and message (``unclosed database in <sqlite3.Connection object at``) before adding the filters.

  This means you can have this in your pytest configuration for complete oblivion (not recommended, if that is not clear)::

    filterwarnings = [
        "error",
        "ignore:unclosed database in <sqlite3.Connection object at:ResourceWarning",
        "ignore::PytestCovWarning",
        "ignore::CoverageWarning",
    ]



6.1.1 (2025-04-05)
------------------

* Fixed breakage that occurs when ``--cov-context`` and the ``no_cover`` marker are used together.

6.1.0 (2025-04-01)
------------------

* Change terminal output to use full width lines for the coverage header.
  Contributed by Tsvika Shapira in `#678 <https://github.com/pytest-dev/pytest-cov/pull/678>`_.
* Removed unnecessary CovFailUnderWarning. Fixes `#675 <https://github.com/pytest-dev/pytest-cov/issues/675>`_.
* Fixed the term report not using the precision specified via ``--cov-precision``.


6.0.0 (2024-10-29)
------------------

* Updated various documentation inaccuracies, especially on subprocess handling.
* Changed fail under checks to use the precision set in the coverage configuration.
  Now it will perform the check just like ``coverage report`` would.
* Added a ``--cov-precision`` cli option that can override the value set in your coverage configuration.
* Dropped support for now EOL Python 3.8.

5.0.0 (2024-03-24)
------------------

* Removed support for xdist rsync (now deprecated).
  Contributed by Matthias Reichenbach in `#623 <https://github.com/pytest-dev/pytest-cov/pull/623>`_.
* Switched docs theme to Furo.
* Various legacy Python cleanup and CI improvements.
  Contributed by Christian Clauss and Hugo van Kemenade in
  `#630 <https://github.com/pytest-dev/pytest-cov/pull/630>`_,
  `#631 <https://github.com/pytest-dev/pytest-cov/pull/631>`_,
  `#632 <https://github.com/pytest-dev/pytest-cov/pull/632>`_ and
  `#633 <https://github.com/pytest-dev/pytest-cov/pull/633>`_.
* Added a ``pyproject.toml`` example in the docs.
  Contributed by Dawn James in `#626 <https://github.com/pytest-dev/pytest-cov/pull/626>`_.
* Modernized project's pre-commit hooks to use ruff. Initial POC contributed by
  Christian Clauss in `#584 <https://github.com/pytest-dev/pytest-cov/pull/584>`_.
* Dropped support for Python 3.7.

4.1.0 (2023-05-24)
---------------