Skip to content

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

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

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

@yangfan-yf-yf yangfan-yf-yf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the late-xdist path on 98cc2f6.

  • python -m pytest tests/test_pytest_cov.py::test_xdist_enabled_after_initial_conftests -q passed on Windows/Python 3.12.
  • Ruff linting and formatting checks pass for the changed plugin and test files, and git diff --check is clean.

The guard is limited to the two distributed-only hooks, preserves the already-started coverage controller, and turns the previously uncaught missing-method path into the project’s existing DistCovError with actionable configuration guidance. The changelog and contributor entries follow the repository contribution checklist.

@pctablet505
pctablet505 marked this pull request as ready for review July 27, 2026 10:42
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

2 participants