gpt4 book ai didi

python - pytest:如何在 session 结束时获取所有失败测试的列表? (并且在使用 xdist 时)

转载 作者:太空狗 更新时间:2023-10-29 20:48:55 26 4
gpt4 key购买 nike

我想要一个所有失败测试的列表,以便在 session 结束时使用。

Pytest 允许您定义一个 Hook pytest_sessionfinish(session, exitstatus),它在 session 结束时调用,我希望在其中获得该列表。

session 是一个 _pytest.main.Session 实例,具有属性 items(类型 list),但我找不到该列表中的每个 item 是否通过或失败。

  1. 如何在 session 结束时检索所有失败测试的列表?
  2. 如何在使用 pytest-xdist 插件时完成,我想在主进程中获取该列表。使用这个插件,session 在 master 中甚至没有 items 属性:

    def pytest_sessionfinish(session, exitstatus):
    if os.environ.get("PYTEST_XDIST_WORKER", "master") == "master":
    print(hasattr(session, "items")) # False

最佳答案

使用 -rf 运行 pytest,让它在最后打印失败测试的列表。

来自 py.test --help:

  -r chars              show extra test summary info as specified by chars
(f)ailed, (E)error, (s)skipped, (x)failed, (X)passed,
(p)passed, (P)passed with output, (a)all except pP.
Warnings are displayed at all times except when
--disable-warnings is set

这是你得到的:

$ py.test -rf
================= test session starts =================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.6.0, pluggy-0.7.1
[...]
=============== short test summary info ===============
FAILED test_foo.py::test_foo_is_flar
FAILED test_spam.py::test_spam_is_mostly_pork
FAILED test_eggs.py::test_eggs_are_also_spam
=== 3 failed, 222 passed, 8 warnings in 12.52 seconds ==

关于python - pytest:如何在 session 结束时获取所有失败测试的列表? (并且在使用 xdist 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48054392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com