Pytest----控制测试文件产生的告警都忽略或者转换为报错
【原文链接】Pytest----如何控制一个测试文件产生的告警都忽略或者转换为报错
在有些场景下,希望对一个测试文件中的所有的测试方法中的告警进行忽略不显示或者将所有的告警转换为错误,此时只需要在文件开头定义pytestmark即可,如下代码中在定义pytestmark中使用ignore参数,表示当前文件中的所有告警都忽略不显示。
import pytest
import warnings
pytestmark = pytest.mark.filterwarnings("ignore")
def test_demo1():
print("in test_demo1 ...")
warnings.warn(SyntaxWarning("warning,used to test..."))
assert 1==1
def test_demo2():
print("in test_demo2 ...")
warnings.warn(UserWarning("warning,used to demo..."))
assert 1==1
执行结果如下,可以看出此时文件中定义的两个告警均为显示。
(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
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items
test_demo.py in test_demo1 ...
.in test_demo2 ...
.
==================== 2 passed in 0.02s ====================
(demo-HCIhX0Hq) E:\demo>
如下,即定义pytestmark的时候指定error,则可以将当前文件中的所有告警均转换为报错。
import pytest
import warnings
pytestmark = pytest.mark.filterwarnings("error")
def test_demo1():
print("in test_demo1 ...")
warnings.warn(SyntaxWarning("warning,used to test..."))
assert 1==1
def test_demo2():
print("in test_demo2 ...")
warnings.warn(UserWarning("warning,used to demo..."))
assert 1==1
执行结果如下所示,即两个用例均报错了,报错原因就是告警的原因。
(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
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items
test_demo.py in test_demo1 ...
Fin test_demo2 ...
F
======================== FAILURES =========================
_______________________ test_demo1 ________________________
def test_demo1():
print("in test_demo1 ...")
> warnings.warn(SyntaxWarning("warning,used to test..."))
E SyntaxWarning: warning,used to test...
test_demo.py:7: SyntaxWarning
_______________________ test_demo2 ________________________
def test_demo2():
print("in test_demo2 ...")
> warnings.warn(UserWarning("warning,used to demo..."))
E UserWarning: warning,used to demo...
test_demo.py:13: UserWarning
================= short test summary info =================
FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
FAILED test_demo.py::test_demo2 - UserWarning: warning,used to demo...
==================== 2 failed in 0.08s ====================
(demo-HCIhX0Hq) E:\demo>
同样,也可以通过指定告警类型对当前文件中的所有告警进行过滤,比如如下对当前文件中的UserWarning类型的告警进行忽略。
import pytest
import warnings
pytestmark = pytest.mark.filterwarnings("ignore::UserWarning")
def test_demo1():
print("in test_demo1 ...")
warnings.warn(SyntaxWarning("warning,used to test..."))
assert 1==1
def test_demo2():
print("in test_demo2 ...")
warnings.warn(UserWarning("warning,used to demo..."))
assert 1==1
执行结果如下,可以看出此时UserWarning的告警已经不显示了,而SyntaxWarning类型的告警依然是显示的。
(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
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items
test_demo.py in test_demo1 ...
.in test_demo2 ...
.
==================== warnings summary =====================
test_demo.py::test_demo1
E:\demo\test_demo.py:7: SyntaxWarning: warning,used to test...
warnings.warn(SyntaxWarning("warning,used to test..."))
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============== 2 passed, 1 warning in 0.02s ===============
(demo-HCIhX0Hq) E:\demo>
同样地这里也是支持当当前文件中的所有告警的内容进行正则匹配,如下即从告警信息中匹配demo字符的,匹配到的就忽略不显示。
import pytest
import warnings
pytestmark = pytest.mark.filterwarnings("ignore:.*demo.*")
def test_demo1():
print("in test_demo1 ...")
warnings.warn(SyntaxWarning("warning,used to test..."))
assert 1==1
def test_demo2():
print("in test_demo2 ...")
warnings.warn(UserWarning("warning,used to demo..."))
assert 1==1
执行结果如下,可以看出此时含有demo的UserWarning类型的告警已经不显示了,而不含demo的SyntaxWarning类型的的告警依然会显示。
(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
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items
test_demo.py in test_demo1 ...
.in test_demo2 ...
.
==================== warnings summary =====================
test_demo.py::test_demo1
E:\demo\test_demo.py:7: SyntaxWarning: warning,used to test...
warnings.warn(SyntaxWarning("warning,used to test..."))
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============== 2 passed, 1 warning in 0.02s ===============
(demo-HCIhX0Hq) E:\demo>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcejag
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13