Skip to content
Open
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
2 changes: 2 additions & 0 deletions changelog/1087.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Distributed runs with collection errors now skip test execution and exit with code 2 (interrupted) by default, matching serial pytest.
Pass ``--continue-on-collection-errors`` to restore the previous behavior of running successfully collected tests and exiting with code 1.
22 changes: 17 additions & 5 deletions src/xdist/dsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ 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._collection_error_count = 0
self._failed_collection_errors: set[tuple[str, str]] = set()
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 @@ -313,6 +314,14 @@ def worker_collectionfinish(
node.gateway.spec, WorkerStatus.CollectionDone, tests_collected=len(ids)
)
if self.sched.collection_is_completed:
if self._collection_error_count and not self.config.getoption(
"continue_on_collection_errors"
):
errors = "error" if self._collection_error_count == 1 else "errors"
self.shouldstop = (
f"{self._collection_error_count} {errors} during collection"
)
return
if (
self.terminal
and not self.sched.has_pending
Expand Down Expand Up @@ -422,10 +431,13 @@ def _failed_worker_collectreport(
node: WorkerController,
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
# Workers can send separately deserialized traceback objects for the
# same error. Use the report contents to recognize duplicate reports.
key = (rep.nodeid, rep.longreprtext)
if key not in self._failed_collection_errors:
self._failed_collection_errors.add(key)
if rep.failed:
self._collection_error_count += 1
self.config.hook.pytest_collectreport(report=rep)
self._handlefailures(rep)

Expand Down
40 changes: 38 additions & 2 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_import():
"""
)
result = pytester.runpytest(p1, "-n1")
assert result.ret == 1
assert result.ret == pytest.ExitCode.INTERRUPTED
result.stdout.fnmatch_lines(
["E *Error: No module named *__import_of_missing_module*"]
)
Expand Down Expand Up @@ -1836,7 +1836,7 @@ def test_collection_crash(pytester: pytest.Pytester) -> None:
assert 0
"""
)
result = pytester.runpytest(p1, "-n1")
result = pytester.runpytest(p1, "-n1", "--continue-on-collection-errors")
assert result.ret == 1
result.stdout.fnmatch_lines(
[
Expand All @@ -1849,6 +1849,42 @@ def test_collection_crash(pytester: pytest.Pytester) -> None:
)


@pytest.mark.parametrize(
"options, expected_exit, expected_passed",
[
((), pytest.ExitCode.INTERRUPTED, 0),
(("--lf", "--last-failed-no-failures", "none"), pytest.ExitCode.INTERRUPTED, 0),
(("--continue-on-collection-errors",), pytest.ExitCode.TESTS_FAILED, 1),
],
)
def test_collection_errors_stop_test_execution(
pytester: pytest.Pytester,
options: tuple[str, ...],
expected_exit: pytest.ExitCode,
expected_passed: int,
) -> None:
pytester.makepyfile(
test_broken="raise RuntimeError('collection failed')",
test_ok="def test_ok(): pass",
)
result = pytester.runpytest("-n2", *options)
assert result.ret == expected_exit
assert result.parseoutcomes().get("passed", 0) == expected_passed
result.stdout.fnmatch_lines(["*RuntimeError: collection failed*"])
if expected_exit == pytest.ExitCode.INTERRUPTED:
result.stdout.fnmatch_lines(["*Interrupted: 1 error during collection*"])


def test_collection_stop_counts_distinct_errors(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
test_first="raise RuntimeError('first collection failure')",
test_second="raise RuntimeError('second collection failure')",
)
result = pytester.runpytest("-n2")
assert result.ret == pytest.ExitCode.INTERRUPTED
result.stdout.fnmatch_lines(["*Interrupted: 2 errors during collection*"])


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