Skip to content

Fix fixture display commands discarding non-zero exit statuses - #15048

Open
lpyu001 wants to merge 1 commit into
pytest-dev:mainfrom
lpyu001:fixture
Open

lpyu001 wants to merge 1 commit into
pytest-dev:mainfrom
lpyu001:fixture

Conversation

@lpyu001

@lpyu001 lpyu001 commented Sep 17, 2026 •

Copy link
Copy Markdown

Preserve the exit status returned by showfixtures() and show_fixtures_per_test() instead of unconditionally returning 0 from pytest_cmdline_main().

Collection errors require additional handling because Session.perform_collect() records them in session.testsfailed without raising. Extracted the existing collection-error check from pytest_runtestloop() into _raise_on_collection_errors() and reused it for the fixture display commands. With --continue-on-collection-errors, the commands now return ExitCode.TESTS_FAILED when collection errors occurred.

Added regression tests for both --fixtures and --fixtures-per-test. Pre-fix:

$ python -m pytest --fixtures test_bad.py
...
ERROR test_bad.py
=============================== 1 error in 0.09s ===============================
exit=0

After the fix, collection errors return ExitCode.INTERRUPTED (2), or ExitCode.TESTS_FAILED (1) with --continue-on-collection-errors. Exit statuses produced by internal errors and pytest.exit() are also preserved instead of being replaced with 0.

Closes #15047.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 17, 2026
… errored

Preserve exit statuses returned by fixture display commands and handle
collection errors consistently with the main pytest execution path.

Co-authored-by: ChatGPT <noreply@openai.com>

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I made some tweaks but it should be good for pytest 10.0.

@bluetech bluetech added this to the 10.0 milestone Sep 17, 2026
@hafizahmedzia

hafizahmedzia commented Oct 19, 2026 •

Copy link
Copy Markdown

Additional checks of @lpyu001's fix compared the status received by pytest_sessionfinish with the final subprocess exit, using both --fixtures and --fixtures-per-test.

On Linux/CPython 3.12.14, comparing 56a92c19d4a71152c594726efc9deb5f0e126e99 with 185e3a7f299308e454afca4c5befb307bc3f139a:

  • Syntax-error collection: hook/process status is 0/0 on the parent and 2/2 on the head.
  • Successful collection followed by a sessionfinish hook calling pytest.exit(returncode=9): the hook receives 0 on both; the parent exits 0 and the head exits 9.
  • Empty-collection controls stay at hook/process 0/0.

All 12 subprocess cases behaved this way. Plugin autoload was disabled, each process recorded the source checkout imported, and test bodies were not executed. This specifically checks preservation of the sessionfinish override. The full pytest suite and CI were not run. This does not change the maintainer's pytest 10.0 timing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--fixtures and --fixtures-per-test exit with code 0 on collection errors, internal errors and pytest.exit()

3 participants