{"id":416,"library":"pytest-cov","title":"pytest-cov: Coverage Plugin for pytest","description":"pytest-cov is a pytest plugin that provides comprehensive code coverage functionality, integrating seamlessly with `coverage.py`. It extends pytest with features like automatic erasing and combination of `.coverage` data files, support for detailed coverage contexts, and compatibility with `pytest-xdist` for distributed testing. It is actively maintained by the pytest-dev team.","status":"active","version":"7.1.0","language":"python","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-cov","tags":["pytest","testing","coverage","code quality","development tools","python"],"install":[{"cmd":"pip install pytest-cov","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core testing framework.","package":"pytest"},{"reason":"Underlying code coverage engine.","package":"coverage"},{"reason":"pytest's plugin system, required version >= 1.2.0 for recent pytest-cov versions.","package":"pluggy","optional":false},{"reason":"For parallel/distributed testing support.","package":"pytest-xdist","optional":true}],"imports":[],"quickstart":{"code":"import pytest\n\n# app.py\ndef greet(name):\n    if name:\n        return f\"Hello, {name}!\"\n    return \"Hello, World!\"\n\n# test_app.py\ndef test_greet_with_name():\n    assert greet(\"Alice\") == \"Hello, Alice!\"\n\ndef test_greet_without_name():\n    assert greet(None) == \"Hello, World!\"\n\n# To run this, save as app.py and test_app.py, then:\n# pytest --cov=app test_app.py\n# You'll see a coverage report in your terminal.\n# For an HTML report: pytest --cov=app --cov-report=html test_app.py","lang":"python","description":"Create a simple Python module (`app.py`) and a test file (`test_app.py`). Run pytest with the `--cov` flag, pointing to the module or directory you want to measure coverage for. This will output a summary to the terminal. You can specify different report formats like HTML, XML, or JSON using `--cov-report`."},"warnings":[{"fix":"Consult `coverage.py` documentation for subprocess configuration. Ensure your `.coveragerc` or `pyproject.toml` correctly specifies `[run] relative_files = true` and `[run] source` paths, and apply appropriate patching.","message":"Subprocess coverage measurement changed significantly in pytest-cov 7.0.0. Older versions (6.3 and below) relied on a `.pth` file, which was removed. For subprocess coverage, you must now explicitly configure `coverage.py`'s patching options (e.g., `coverage run --rcfile=.coveragerc -m pytest`).","severity":"breaking","affected_versions":"7.0.0 and later"},{"fix":"Avoid using `pytest-cov` for direct multiprocessing coverage. If parallel execution is needed, `pytest-xdist` is supported, but for `coverage.py` itself across truly separate processes, manual combining of `.coverage` files might be necessary or explore `coverage.py`'s own capabilities.","message":"Support for multiprocessing was dropped in pytest-cov 4.0.0 (released September 28, 2022) due to inherent flakiness and maintenance challenges.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"Verify the `--cov` target path is correct and points to your application code's root directory or package. Check your `.coveragerc` or `pyproject.toml` for any exclusions (`[run] omit`, `[report] exclude_lines`) that might be inadvertently blocking files. As a workaround or alternative, consider running tests directly via `coverage run -m pytest`.","message":"Users often encounter 'No data was collected' errors, especially after reorganizing tests into nested subdirectories. This indicates `pytest-cov` couldn't trace your application code.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install the latest `pytest-cov` version compatible with your project's Python interpreter. Use a `requirements.txt` with version pins or consult the `pytest-cov` documentation/changelog for specific compatibility matrices.","message":"Specific `pytest-cov` versions might be required for compatibility with older Python versions. For example, Python 3.8 typically requires `pytest-cov < 6.0.0`, and Python 3.7 requires `pytest-cov < 5.0.0`.","severity":"gotcha","affected_versions":"Older Python versions (e.g., 3.6, 3.7, 3.8)"},{"fix":"Consolidate your coverage configuration in a `.coveragerc` file (or `[tool.coverage]` section in `pyproject.toml`). `pytest-cov` will automatically pick up these settings, providing a cleaner `pytest` command line and more maintainable configuration.","message":"While `pytest-cov` offers command-line flags for reporting, complex or persistent coverage configurations (e.g., custom inclusions/exclusions, minimum coverage thresholds) are best managed in `coverage.py`'s dedicated configuration file (`.coveragerc` or `pyproject.toml`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"pip install pytest-cov","cause":"The pytest-cov plugin is not installed in the current Python environment or is not discoverable by pytest.","error":"pytest: error: unrecognized arguments: --cov"},{"fix":"Ensure the path provided to `--cov` (e.g., `pytest --cov=my_package tests/`) is correct and that the tests actually execute code within the specified package or module.","cause":"pytest-cov was unable to collect any coverage data for the specified target, often because the `--cov` path is incorrect, or the tests do not execute code within that path.","error":"No data to report."},{"fix":"pip install coverage","cause":"The underlying `coverage.py` library, which `pytest-cov` depends on, is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'coverage'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}