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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值