Repository navigation
Move collection error abort from pytest_runtestloop to pytest_collection - #15384
megacodes24 wants to merge 2 commits into
Conversation
|
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it. The move itself looks right, and the docs fix over #15061 checks out (its RTD build failed, this one passes). One thing blocks it: pytest-xdist. xdist workers implement their own The run does stop, which is what #15028 wants, but through a restart storm with a misleading message. So this needs a matching change in pytest-xdist before it can be merged, most likely the controller handling a collection abort from a worker as a clean stop instead of a crash. Are you interested in doing the xdist side too? Otherwise we can take it from here. The changelog should also mention plugins that run collection in worker processes. Generated by Claude Code |
|
Thanks for testing it with xdist! Yes, I'd like to do the xdist side too. My plan, following how pytest-dev/pytest-xdist#1408 handles usage errors: in remote.py's pytest_sessionfinish, when the worker session ends with Interrupted from the collection check (and not a real KeyboardInterrupt), record that in workeroutput. Then in dsession.worker_workerfinished, treat it as a clean stop (set shouldstop, remove the node, trigger shutdown) instead of the keyboard-interrupt/restart path. For the test, I'd use a conftest pytest_collection wrapper that does the same check, so it works with the current pytest release too. I'll also update the changelog here to mention plugins that run collection in worker processes. Does that sound right? |
|
Update: I have a working fix for the xdist side. One change from the plan above: pytest catches Interrupted in wrap_session without re-raising it, so sys.exc_info() is empty in pytest_sessionfinish. Instead the worker records it in pytest_keyboard_interrupt, only for Session.Interrupted, so a real Ctrl+C still takes the existing path. With it, pytest -n2 with a collection error stops with Interrupted: 1 error during collection, with no worker restarts, and the passing test is no longer reported as "crashed while running". The xdist test suite passes with pytest 9.2.0 and with this branch. Three existing xdist tests needed a version check, since the exit code becomes 2 instead of 1 with pytest 10. I'll open it as a draft PR on pytest-xdist and link it here. The changelog here is updated too. Draft PR: pytest-dev/pytest-xdist#1439 |
Closes #15028.
The check that stops the session after collection errors lived in the default
pytest_runtestloop. Since that hook isfirstresult, a plugin that implements its ownpytest_runtestloopnever reached the check, and tests ran even though collection failed. As suggested by @bluetech in the issue, this moves the check into the defaultpytest_collection.This picks up #15061 by @gyanu2507, which the author closed and invited others to continue. Compared to that PR:
pytest_collectiondocstring,Interruptedis written as a literal instead of:class:~pytest.Interrupted.Interruptedis not exported from thepytestnamespace, so I believe that reference is what failed the Read the Docs build there (the docs build is nitpicky).--continue-on-collection-errors, a plugin'spytest_runtestloopstill runs.Changes:
src/_pytest/main.py: moved thetestsfailedcheck frompytest_runtestlooptopytest_collection.src/_pytest/hookspec.py: updated thepytest_collectionandpytest_runtestloopdocstrings.testing/test_collection.py: two new tests.changelog/15028.breaking.rst: breaking change entry, since the current behavior is documented.How I tested (Windows, Python 3.12):
Reproduced the issue with a small project whose
conftest.pyreplacespytest_runtestloopand a test file that fails to import. Before the change:1 passed, 1 error. After:Interrupted: 1 error during collection.With the original
main.py, the first new test fails (INTERNAL_ERRORinstead ofINTERRUPTED), and it passes with the change.testing/test_collection.py: 109 passed.test_main.py,test_terminal.py,acceptance_test.pypass excepttest_pydoc, which also fails onmainon my machine (UnicodeDecodeError, a Windows encoding issue unrelated to this change).pre-commit passes on the changed files.
Include new tests or update existing tests when applicable.
Allow maintainers to push and squash when merging my commits.
Add text like
closes #XYZWto the PR description and/or commits.If AI agents were used, they are credited in
Co-authored-bycommit trailers.Create a new changelog file in the
changelogdirectory.Add yourself to
AUTHORSin alphabetical order.