Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1140.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed duplicate ``pytest_collectreport`` calls (one per worker) for the same collection error when a test module fails to collect under ``-n``.
10 changes: 6 additions & 4 deletions src/xdist/dsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, config: pytest.Config) -> None:
self.maxfail: int = config.getvalue("maxfail")
self.queue: Queue[tuple[str, dict[str, Any]]] = Queue()
self._session: pytest.Session | None = None
self._failed_collection_errors: dict[object, bool] = {}
self._failed_collection_errors: dict[str, bool] = {}
self._active_nodes: set[WorkerController] = set()
self._failed_nodes_count = 0
self._max_worker_restart = get_default_max_worker_restart(self.config)
Expand Down Expand Up @@ -409,9 +409,11 @@ def _failed_worker_collectreport(
rep: pytest.CollectReport | pytest.TestReport,
) -> None:
# Check we haven't already seen this report (from
# another worker).
if rep.longrepr not in self._failed_collection_errors:
self._failed_collection_errors[rep.longrepr] = True
# another worker). Keyed by nodeid, not longrepr: pytest's
# longrepr classes are `eq=False` dataclasses, so identical
# errors from different workers never compare equal.
if rep.nodeid not in self._failed_collection_errors:
self._failed_collection_errors[rep.nodeid] = True
self.config.hook.pytest_collectreport(report=rep)
self._handlefailures(rep)

Expand Down
14 changes: 14 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,20 @@ def test_collection_crash(pytester: pytest.Pytester) -> None:
)


def test_collection_crash_reported_once(pytester: pytest.Pytester) -> None:
"""A collection error must be reported only once, regardless of how
many workers collected the failing module (#1140)."""
p1 = pytester.makepyfile(
"""
assert 0
"""
)
result = pytester.runpytest(p1, "-n2")
assert result.ret == 1
assert result.stdout.lines.count("E assert 0") == 1
result.stdout.fnmatch_lines(["*= 1 error in *"])


def test_dist_in_addopts(pytester: pytest.Pytester) -> None:
"""Users can set a default distribution in the configuration file (#789)."""
pytester.makepyfile(
Expand Down