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

标签 python unit-testing testing pytest xdist

我想要一个所有失败测试的列表,以便在 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/

相关文章:

python - 合并序列的生成器的问题

unit-testing - 基于 Spock 交互的测试 : too few invocation on a method

java - 如果我们使用 Selenium Webdriver,我们如何捕获测试用例结果?

java - 在phpbb3中获取form_token和time_creation

Python正则表达式: remove non-ASCII characters and words ending in number

python - x, = ... - 这个尾随逗号是逗号运算符吗?

python - 如何在Python中实现b进制扩展?

java - 使用 ExecutorService 运行时,Mockito 验证 "times"显示的计数少于实际计数

javascript - 在关于 expect.throw 的链中进行单元测试时遇到问题

c# - 在 asp.net mvc5 中针对 asp.net 身份模拟 IUserStore