Skip to content

Raise a clear error when xdist is enabled after pytest-cov has started#749

Draft
pctablet505 wants to merge 1 commit into
pytest-dev:masterfrom
pctablet505:fix-xdist-configure-node-crash-740
Draft

Raise a clear error when xdist is enabled after pytest-cov has started#749
pctablet505 wants to merge 1 commit into
pytest-dev:masterfrom
pctablet505:fix-xdist-configure-node-crash-740

Conversation

@pctablet505

Copy link
Copy Markdown

Summary

Fixes #740.

When another plugin only enables pytest-xdist after pytest-cov's own
pytest_load_initial_conftests has already run (for example, a plugin that
adds -n/--dist based on addopts read from a config file), pytest-cov
has already committed to centralised (Central) coverage measurement by
the time xdist calls pytest_configure_node. That produced a confusing
AttributeError: 'Central' object has no attribute 'configure_node'.

Coverage measurement for the master process has to start very early (during
pytest_load_initial_conftests) so that code imported while conftests load
is still measured, so we can't reliably know in advance whether xdist will
be turned on later by some other plugin. Rather than silently swapping
engines mid-run (which would mean discarding coverage already collected),
this detects the mismatch when it happens and raises the existing
DistCovError with a message that explains what happened and how to avoid
it.

Added a regression test that simulates a plugin enabling xdist late, and
confirmed it reproduces the original AttributeError without the fix and
raises a clean DistCovError with it.

Test plan

  • Added test_xdist_enabled_after_initial_conftests in tests/test_pytest_cov.py
  • Verified the new test fails with the original AttributeError on master
  • Ran the full test suite (pytest tests/test_pytest_cov.py); all tests pass except
    test_celery, which fails identically on master due to a missing testcontainers/Docker
    dependency in my environment, unrelated to this change
  • ruff check and ruff format --check pass on the touched files

If another plugin enables pytest-xdist only after pytest-cov's own
pytest_load_initial_conftests has already run (for example a plugin that
reacts to addopts from a config file), pytest-cov had already committed
to centralised coverage measurement. When xdist then called
pytest_configure_node, this crashed with a cryptic
"AttributeError: 'Central' object has no attribute 'configure_node'".

Detect this mismatch and raise the existing DistCovError with an
actionable message instead.

Fixes pytest-dev#740
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.

Combining pytest-cov and pytest-xdist leads to internal errors on setuptools

1 participant