You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect mixture of exit code and jUnit report when pytest.exit is called and a fixture teardown fails #8750
importpytest@pytest.fixture(scope="function")defmyfixture():
print("enter fixture")
yieldassertFalse, "fixture fails in teardown"deftest_foo(myfixture):
pytest.exit("foo")
Run this with pytest --junit-xml=junit.xml. The exit code is 1, which, per the documentation, means “some of the tests failed”. However, if you examine junit.xml, it does not record any failed tests! I’m not sure whether the exit code ought to be 2 (because of the pytest.exit call) or whether the test teardown ought to be recorded as a failure in junit.xml (because of the assertion failure, and the exit code remain 1 in this case), and I don’t really care that much which one happens, but it ought to be one or the other.
pytest.exit is meant to be "uncleanly tear down the process" -- I'd be surprised if any useful junit is written in that case (I don't expect it to). pytest.exit isn't meant to be used in general (more for plugins, or extremely exceptional cases) -- pytest.fail is probably more appropriate or just assert.
I debugged this, junitxml writes the file during pytest_sessionfinish, which is called even when pytest.exit() is executed.
However junitxml never receives the pytest_runtest_reportlog hook with the fixture assertion failure, because of the order pytest_sessionfinish is called: when executing the teardown procedure in wrap_session, first junitxml.pytest_sessionfinish is called, which writes out the XML file, and later runner.pytest_sessionfinish is called, which tear downs the fixture and issues the pytest_runtest_reportlog with the fixture failure.
pytest.exit isn't meant to be used in general (more for plugins, or extremely exceptional cases) -- pytest.fail is probably more appropriate or just assert.
User code can definitely call it (not only plugins), however as @asottile mentions, it is more of an "unclean exit" so some hooks might not get called.
I realize pytest.exit isn’t the nicest and cleanest thing to use, but in this specific case, I think it’s appropriate: I call it if a particular test fails in a particular way that leaves external hardware in a state where the rest of the scheduled tests can’t usefully run, and their results would be misleading if they were to do so.
It seems to me that an exit due to pytest.exit probably ought to be 2 rather than 1 in this case, since pytest.exit is more important than a fixture failing; however, I don’t know how easy it is to plumb the necessary information through to the place where the process exit code is calculated as I’m not very familiar with pytest’s internals.
Hi! I realize this is an older issue, but is it still something the maintainers would like to fix? If so, I'd be happy to take it on.
I reproduced it against current main (0212785, Python 3.14.4): calling pytest.exit() followed by a fixture teardown failure exits with status 1, while the JUnit XML records zero errors and zero failures.
Before preparing a patch, I'd appreciate confirmation that a fix is welcome and any guidance on the intended behavior when the requested exit and a teardown error occur together.
Activity
asottile commentedon Jun 12, 2021
pytest.exitis meant to be "uncleanly tear down the process" -- I'd be surprised if any useful junit is written in that case (I don't expect it to).pytest.exitisn't meant to be used in general (more for plugins, or extremely exceptional cases) --pytest.failis probably more appropriate or justassert.nicoddemus commentedon Jun 12, 2021
I debugged this,
junitxmlwrites the file duringpytest_sessionfinish, which is called even whenpytest.exit()is executed.However
junitxmlnever receives thepytest_runtest_reportloghook with the fixture assertion failure, because of the orderpytest_sessionfinishis called: when executing the teardown procedure inwrap_session, firstjunitxml.pytest_sessionfinishis called, which writes out the XML file, and laterrunner.pytest_sessionfinishis called, which tear downs the fixture and issues thepytest_runtest_reportlogwith the fixture failure.User code can definitely call it (not only plugins), however as @asottile mentions, it is more of an "unclean exit" so some hooks might not get called.
I don't see a clean solution to this, I'm afraid.
Hawk777 commentedon Jun 20, 2021
I realize
pytest.exitisn’t the nicest and cleanest thing to use, but in this specific case, I think it’s appropriate: I call it if a particular test fails in a particular way that leaves external hardware in a state where the rest of the scheduled tests can’t usefully run, and their results would be misleading if they were to do so.It seems to me that an exit due to
pytest.exitprobably ought to be 2 rather than 1 in this case, sincepytest.exitis more important than a fixture failing; however, I don’t know how easy it is to plumb the necessary information through to the place where the process exit code is calculated as I’m not very familiar with pytest’s internals.TonyRolfe commentedon Nov 1, 2026
Hi! I realize this is an older issue, but is it still something the maintainers would like to fix? If so, I'd be happy to take it on.
I reproduced it against current main (0212785, Python 3.14.4): calling pytest.exit() followed by a fixture teardown failure exits with status 1, while the JUnit XML records zero errors and zero failures.
Before preparing a patch, I'd appreciate confirmation that a fix is welcome and any guidance on the intended behavior when the requested exit and a teardown error occur together.