From 7355149b2c27332697e27d39f53917a8689db2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Vokr=C3=A1=C4=8Dko?= Date: Wed, 29 Apr 2026 14:10:37 +0200 Subject: [PATCH] build: measure coverage on source only via coverage run The previous setup relied on pytest-cov (``--cov pytest_split --cov tests`` in addopts) which has a long-standing limitation when measuring a pytest plugin: the plugin imports happen during pytest's plugin discovery, *before* pytest-cov starts tracing. Anything that runs at import time (class bodies, decorators, type aliases, enum definitions) is reported as uncovered even though it obviously executed - the plugin wouldn't load otherwise. The ``CoverageWarning: Module pytest_split was previously imported, but not measured`` made that explicit. The cookiecutter template the project was generated from worked around this by adding ``--cov tests`` to the addopts, inflating the denominator with 100%-covered test files so the average reached 90%. That pulled the number up but stopped reporting the actual source coverage. Switch to ``coverage run -m pytest`` followed by ``coverage report``. Running ``coverage`` from the outside means tracing starts before Python imports anything at all, so plugin import-time code is counted. Real source coverage jumps from ~74% to 99%; threshold goes from 90 to 95 to reflect that. --- .github/workflows/test.yml | 3 ++- pyproject.toml | 12 ++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f2dbbdd..505b3ad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,7 +44,8 @@ jobs: - uses: ./.github/actions/python-poetry-env with: python-version: ${{ matrix.python-version }} - - run: poetry run pytest + - run: poetry run coverage run -m pytest + - run: poetry run coverage report docs: runs-on: ubuntu-latest diff --git a/pyproject.toml b/pyproject.toml index be14d7d..164e854 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,16 +63,12 @@ pytest-split = "pytest_split.plugin" target-version = ["py310", "py311", "py312", "py313", "py314"] include = '\.pyi?$' -[tool.pytest.ini_options] -addopts = """\ - --cov pytest_split \ - --cov tests \ - --cov-report term-missing \ - --no-cov-on-fail \ -""" +[tool.coverage.run] +source = ["pytest_split"] [tool.coverage.report] -fail_under = 90 +fail_under = 95 +show_missing = true exclude_lines = [ 'if TYPE_CHECKING:', 'pragma: no cover'