【原文链接】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>