Skip to content

using pytest-cov to measure coverage of pytest plugins #682

Closed

Description

@cjw296

Summary

Is it expected that pytest-cov will record coverage for pytest plugins?

Reproducer

I'm attempting to get coverage for https://github.com/toirl/pytest-sqlalchemy up to 100%.
The current test coverage is, er, minimal ;-)

Running uv run --all-extras --dev --resolution highest -m pytest --cov --sqlalchemy-connect-url=$DB_URL appears to record no coverage information other than that from imports:

============================= test session starts ==============================
platform linux -- Python 3.9.21, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/runner/work/pytest-sqlalchemy/pytest-sqlalchemy
configfile: pyproject.toml
plugins: sqlalchemy-0.2.0, cov-6.0.0
collected 1 item

tests/test_sqlite.py .                                                   [100%]

---------- coverage: platform linux, python 3.9.21-final-0 -----------
Name                   Stmts   Miss  Cover
------------------------------------------
pytest_sqlalchemy.py      83     54    [35](https://github.com/toirl/pytest-sqlalchemy/actions/runs/14125161610/job/39572569617#step:4:36)%
tests/test_sqlite.py       2      0   100%
------------------------------------------
TOTAL                     85     54    [36](https://github.com/toirl/pytest-sqlalchemy/actions/runs/14125161610/job/39572569617#step:4:37)%

...so I was curious if this is an intended use case or if I should head for coverage run -m pytest instead?

Activity

  1. ionelmc commented on Mar 31, 2025

    @ionelmc
    Member

    Yes and no. I tried to document a workaround here https://pytest-cov.readthedocs.io/en/latest/plugins.html

    You can always just use coverage run directly. The tradeoff here is that the cov plugin implements a proper context for coverage (if you use that) while using coverage directly doesn't really set a good context (dynamic_context = test_function is very basic). See https://coverage.readthedocs.io/en/latest/contexts.html

  2. cjw296 commented on Apr 10, 2025

    @cjw296
    Author

    Yeah, I ended up going with coverage run in the end, it's very meta when you're using pytester to run tests with a plugin, under xist 😅.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions