首页 > 解决方案 > 停止 pytest 错误但不失败

问题描述

Pytest 报告error是否在初始化夹具中断言失败,以及fail在初始化夹具运行后测试是否失败。

要使测试套件在错误或失败时停止,可以使用以下标志:

-x, --exitfirst exit instantly on first error or failed test.

如何在第一个错误后停止测试套件,但如果出现故障则继续运行?

标签: pythonpytest

解决方案


这应该不难实现自己。一个最小的例子(未经测试):

# conftest.py

from _pytest.main import Failed


def pytest_runtest_logreport(report):
    if report.failed and report.when in ('setup', 'teardown'):
        raise Failed()

这将在测试失败 ( report.failed and report.when == 'call') 时继续,但在设置/拆卸错误时停止。


推荐阅读