对告警进行断言,可以通过with语句对告警的类型进行断言,如下,即断言代码中会产生UserWarning类型的告警。
import pytest
import warnings
def test_demo():
print("in test_demo ...")
with pytest.warns(UserWarning):
warnings.warn(UserWarning("warning,used to demo..."))
执行结果如下,即断言通过。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item
test_demo.py in test_demo ...
.
==================== 1 passed in 0.02s ====================
(demo-HCIhX0Hq) E:\demo>
将测试代码修改为如下代码,即在代码中抛出SyntaxWarning类型的告警,而断言期望的是UserWarning类型的告警。
import pytest
import warnings
def test_demo():
print("in test_demo ...")
with pytest.warns(UserWarning):
warnings.warn(SyntaxWarning("warning,used to demo..."))
执行结果如下,即此时报错了,显示并未有UserWarning类型的告警产生。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item
test_demo.py in test_demo ...
F
======================== FAILURES =========================
________________________ test_demo ________________________
def test_demo():
print("in test_demo ...")
with pytest.warns(UserWarning):
> warnings.warn(SyntaxWarning("warning,used to demo..."))
E Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) were emitted.
E The list of emitted warnings is: [SyntaxWarning('warning,used to demo...')].
test_demo.py:8: Failed
================= short test summary info =================
FAILED test_demo.py::test_demo - Failed: DID NOT WARN. No warnings of type (<class 'User...
==================== 1 failed in 0.07s ====================
(demo-HCIhX0Hq) E:\demo>
此外,还可以通过正则表达式对告警内容进行断言,如下即判断告警内容中含有demo。
import pytest
import warnings
def test_demo():
print("in test_demo ...")
with pytest.warns(UserWarning,match=r'.*demo.*'):
warnings.warn(UserWarning("warning,used to demo..."))
执行结果如下,即断言通过。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item
test_demo.py in test_demo ...
.
==================== 1 passed in 0.02s ====================
(demo-HCIhX0Hq) E:\demo>
初次以为,还可以对函数调用中的告警类型进行断言,如下即断言add函数中会产生UserWarning类型的告警,同时,add函数需要给定两个参数a和b。
import pytest
import warnings
def add(a,b):
print("a:",a)
print("b:",b)
warnings.warn(UserWarning("warning,used to demo..."))
def test_demo():
print("in test_demo ...")
pytest.warns(UserWarning,add,10,20)
执行结果如下,可以看出,参数a和b能够正常传入,而且也做到了对函数中产生的告警类型的断言。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item
test_demo.py in test_demo ...
a: 10
b: 20
.
==================== 1 passed in 0.02s ====================
(demo-HCIhX0Hq) E:\demo>