pytest-xdist
pytest-xdist copied to clipboard
pytest_sessionfinish hook executes before the last worker is done
pytest_sessionfinish is a built-in pytest hook method that executes after all tests finish.
Called after whole test run finished, right before returning the exit status to the system.
pytest-xdist violates the intended usage of the function by calling the method while workers are still running, rather than "right before returning the exit status to the system". It does not run multiple times for each worker, so it appears something internal to pytest-xdist executes this before all workers are done.
If this is a desired outcome it would be beneficial to document it.
I am having a similar issue, I want to access the session data and retrieve the failed tests using this in conftest
def pytest_sessionfinish(session, exitstatus):
if exitstatus == 1:
for sess in session.items:
if sess._test_failed_statuses["call"]:
payload["description"] += f"\n- {sess.nodeid}"
But when running with xdist I am getting this error :
line 162, in pytest_sessionfinish
for sess in session.items:
AttributeError: 'Session' object has no attribute 'items'
What is the work around to access the session data for all the tests.
我也遇到了这个问题pytest_sessionfinish会被多个worker执行,而不是在整个测试流程结束之后执行一次
xdist will have one sessionfinish called per worker, hopefully we can distinguish workers and controller, so the issue can easily be worked around using:
def pytest_sessionfinish(session, exitstatus):
if xdist.is_xdist_worker(session):
return
... # the actual sessionfinish code