Raise a clear error when xdist is enabled after pytest-cov has started#749
Draft
pctablet505 wants to merge 1 commit into
Draft
Raise a clear error when xdist is enabled after pytest-cov has started#749pctablet505 wants to merge 1 commit into
pctablet505 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #740.
When another plugin only enables pytest-xdist after pytest-cov's own
pytest_load_initial_conftestshas already run (for example, a plugin thatadds
-n/--distbased onaddoptsread from a config file), pytest-covhas already committed to centralised (
Central) coverage measurement bythe time xdist calls
pytest_configure_node. That produced a confusingAttributeError: '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 loadis 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
DistCovErrorwith a message that explains what happened and how to avoidit.
Added a regression test that simulates a plugin enabling xdist late, and
confirmed it reproduces the original
AttributeErrorwithout the fix andraises a clean
DistCovErrorwith it.Test plan
test_xdist_enabled_after_initial_conftestsintests/test_pytest_cov.pyAttributeErroronmasterpytest tests/test_pytest_cov.py); all tests pass excepttest_celery, which fails identically onmasterdue to a missingtestcontainers/Dockerdependency in my environment, unrelated to this change
ruff checkandruff format --checkpass on the touched files