pytest:如何在会话结束时获取所有失败测试的列表?(并且在使用 xdist 时) [英] pytest: How to get a list of all failed tests at the end of the session? (and while using xdist)
问题描述
我想要一个所有失败测试的列表,以便在会话结束时使用.
I would like to have a list of all the tests that have failed to be used at the end of session.
Pytest 允许您定义一个钩子 pytest_sessionfinish(session, exitstatus)
,它在会话结束时被调用,我希望在那里获得该列表.
Pytest lets you define a hook pytest_sessionfinish(session, exitstatus)
, that is called at the end of the session, where I wish to have that list.
session
是一个 _pytest.main.Session
实例,它具有属性 items
(类型 list
),但我找不到该列表中的每个 item
是否通过失败.
session
is a _pytest.main.Session
instance that has the attribute items
(type list
), but I couldn't find whether the each item
in that list passed of failed.
- 如何在会话结束时检索所有失败测试的列表?
如何在使用
pytest-xdist
插件时完成,我想在主进程中获取该列表.使用这个插件,session
在 master 中甚至没有items
属性:
- How can a list of all failed tests could be retrieved at the end of the session?
How can it be done while using
pytest-xdist
plugin, where I would like to get that list in the master process. Using this plugin,session
does not even haveitems
attribute in the master:
def pytest_sessionfinish(session, exitstatus):
if os.environ.get("PYTEST_XDIST_WORKER", "master") == "master":
print(hasattr(session, "items")) # False
推荐答案
如果你想要测试的结果,你可以使用 hook runtest_makereport
:
If you want results of the tests you can use hook runtest_makereport
:
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
if rep.when == 'call' and rep.failed:
mode = 'a' if os.path.exists('failures') else 'w'
try: # Just to not crash py.test reporting
pass # the test 'item' failed
except Exception as e:
pass
这篇关于pytest:如何在会话结束时获取所有失败测试的列表?(并且在使用 xdist 时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!