Skip to content

Terminate the test loop if shouldfail or shouldstop is set #1026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion src/xdist/dsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def worker_workerfinished(self, node):
self.shouldstop = f"{node} received keyboard-interrupt"
self.worker_errordown(node, "keyboard-interrupt")
return
if node in self.sched.nodes:
if not self.shouldstop and node in self.sched.nodes:
crashitem = self.sched.remove_node(node)
assert not crashitem, (crashitem, node)
self._active_nodes.remove(node)
Expand Down
2 changes: 2 additions & 0 deletions src/xdist/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def pytest_runtestloop(self, session):
self.nextitem_index = self._get_next_item_index()
while self.nextitem_index is not self.SHUTDOWN_MARK:
self.run_one_test()
if session.shouldfail or session.shouldstop:
break
return True

def run_one_test(self):
Expand Down
30 changes: 24 additions & 6 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ def test_skip():
)
assert result.ret == 1

def test_exitfail_waits_for_workers_to_finish(
def test_exitfirst_waits_for_workers_to_finish(
self, pytester: pytest.Pytester
) -> None:
"""The DSession waits for workers before exiting early on failure.

When -x/--exitfail is set, the DSession wait for the workers to finish
When -x/--exitfirst is set, the DSession wait for all workers to finish
before raising an Interrupt exception. This prevents reports from the
faiing test and other tests from being discarded.
"""
Expand All @@ -138,15 +138,16 @@ def test_fail6():
time.sleep(0.3)
"""
)
# Two workers are used
result = pytester.runpytest(p1, "-x", "-rA", "-v", "-n2")
assert result.ret == 2
# DSession should stop when the first failure is reached. Two failures
# may actually occur, due to timing.
result.stdout.re_match_lines([".*Interrupted: stopping.*[12].*"])
m = re.search(r"== (\d+) failed, (\d+) passed in ", str(result.stdout))
m = re.search(r"== (\d+) failed", str(result.stdout))
assert m
n_failed, n_passed = (int(s) for s in m.groups())
n_failed = int(m.groups()[0])
assert 1 <= n_failed <= 2
assert 1 <= n_passed <= 3
assert (n_passed + n_failed) < 6

def test_basetemp_in_subprocesses(self, pytester: pytest.Pytester) -> None:
p1 = pytester.makepyfile(
Expand Down Expand Up @@ -1180,6 +1181,23 @@ def test_aaa1(crasher):
assert "INTERNALERROR" not in result.stderr.str()


def test_maxfail_causes_early_termination(pytester: pytest.Pytester) -> None:
"""
Ensure subsequent tests on a worker aren't run when using --maxfail (#1024).
"""
pytester.makepyfile(
"""
def test1():
assert False

def test2():
pass
"""
)
result = pytester.runpytest_subprocess("--maxfail=1", "-n 1")
result.stdout.re_match_lines([".* 1 failed in .*"])


def test_internal_errors_propagate_to_controller(pytester: pytest.Pytester) -> None:
pytester.makeconftest(
"""
Expand Down