Skip to content

fix: resolve coverage config relative to pytest's rootdir (#692) - #765

Open
DawnofGenX wants to merge 1 commit into
pytest-dev:masterfrom
DawnofGenX:fix-cov-config-rootdir-692
Open

DawnofGenX wants to merge 1 commit into
pytest-dev:masterfrom
DawnofGenX:fix-cov-config-rootdir-692

Conversation

@DawnofGenX

Copy link
Copy Markdown

Problem

Coverage resolves a relative config_file (the default .coveragerc, plus the
pyproject.toml / setup.cfg / tox.ini fallbacks) and a relative data_file
against the current working directory. Running pytest from a subdirectory
therefore silently ignores the project's coverage configuration:

# pyproject.toml at the project root
[tool.coverage.report]
exclude_also = ['def __repr__']
  • cd project && pytest tests/ → exclude_also applies (100%).
  • cd project/tests && pytest . → exclude_also is ignored, and .coverage
    is written into tests/ rather than the project root.

Changes

Pass pytest's rootdir through to the controllers and use it as the anchor for
coverage's config/data lookup. It only differs from the cwd in exactly the case
that is broken (running from a subdirectory), so root-level invocations are
unaffected. An explicit --cov-config still wins.

Verification

  • New test test_config_at_rootdir_used_from_subdirectory fails on main
    (the excluded line is reported missing) and passes with the fix.
  • Full suite in a clean environment: 206 passed, 3 skipped. The only failure,
    test_celery, is pre-existing here (celery is not installed) and fails
    identically on main.

…#692)

Coverage resolves a relative `config_file` (the default `.coveragerc`, and
the `pyproject.toml` / `setup.cfg` / `tox.ini` fallbacks) and a relative
`data_file` against the current working directory. Running pytest from a
subdirectory therefore silently ignored the project's coverage configuration
(e.g. `[tool.coverage.report] exclude_also`), and wrote `.coverage` into the
subdirectory instead of the project root.

Pass pytest's rootdir to the controllers and use it as the anchor when no
explicit `--cov-config` was given; it only differs from the cwd in exactly
that case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant