stop pytest tests if several tests have a specific exception(如果多个测试有特定异常,则停止pytest测试)
问题描述
如果任何测试因特定异常而失败,我希望使用pytest.exit()停止测试套件。
例如: 50个测试,它们中的任何一个都可能在某个点因该异常而失败,如果这些测试中至少有2个在此异常情况下失败,我希望停止执行。
我已尝试在测试之间保留全局计数器(带有scope='session'的装置),并在每次捕获此异常时更新它,但无法在测试之间保留它的值。
有什么想法吗?
推荐答案
这可以通过使用Hooks实现。
具体地说,我们将利用两个特定的挂钩,pytest_sessionstart和pytest_exception_interact。我们将使用pytest_sessionstart跟踪我们愿意容忍的特定例外的数量,将其视为存储您提到的全局计数器的位置。另一个钩子pytest_exception_interact将用于与失败的测试交互,以检查返回的异常类型(如果有)。
在测试文件夹的根目录下创建conftest.py文件,并放置以下内容:
import pytest
EXC_MAP = {
ZeroDivisionError: 1,
KeyError: 1
}
def pytest_sessionstart(session):
session.__exc_limits = EXC_MAP
def pytest_exception_interact(node, call, report):
session = node.session
type_ = call.excinfo.type
if type_ in session.__exc_limits:
if session.__exc_limits[type_] == 0:
pytest.exit(f"Reached max exception for type: {type_}")
else:
session.__exc_limits[type_] -= 1
EXC_MAP是我们愿意在测试调用中允许的异常-->;失败的映射。在pytest_sessionstart挂接中,我们在session上设置了一个私有变量来跟踪这些变量。在pytest_exception_interact挂接中,我们获取测试引发的异常类型,对照阈值进行检查,如果该异常的计数达到0,则退出pytest,跳过剩余的测试。
下面是终端中的示例测试脚本和输出。
def foo(a, b):
return a / b
def test_foo():
result = foo(1, 0)
assert result == 1
def test_foo1():
result = foo(1, 1)
assert result == 1
def test_foo2():
result = foo(1, 0)
assert result == 1
def test_foo3():
result = foo(1, 1)
assert result == 1
运行这些命令时,终端显示:
collected 4 items
test_foo.py F.F
========================================================================== FAILURES ==========================================================================
__________________________________________________________________________ test_foo __________________________________________________________________________
def test_foo():
> result = foo(1, 0)
test_foo.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
a = 1, b = 0
def foo(a, b):
> return a / b
E ZeroDivisionError: division by zero
test_foo.py:2: ZeroDivisionError
_________________________________________________________________________ test_foo2 __________________________________________________________________________
def test_foo2():
> result = foo(1, 0)
test_foo.py:16:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
a = 1, b = 0
def foo(a, b):
> return a / b
E ZeroDivisionError: division by zero
test_foo.py:2: ZeroDivisionError
================================================================== short test summary info ===================================================================
FAILED test_foo.py::test_foo - ZeroDivisionError: division by zero
FAILED test_foo.py::test_foo2 - ZeroDivisionError: division by zero
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! _pytest.outcomes.Exit: Reached max exception for type: <class 'ZeroDivisionError'> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================ 2 failed, 1 passed in 0.20s =================================================================
我们可以看到,它收集了所有4个测试,但只运行了其中3个,因为在运行最终测试之前已达到ZeroDivisionError的阈值。
这篇关于如果多个测试有特定异常,则停止pytest测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果多个测试有特定异常,则停止pytest测试
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
