• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest----控制测试文件产生的告警都忽略或者转换为报错

武飞扬头像
redrose2100
帮助1

【原文链接】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
系列文章
更多 icon
同类精品
更多 icon
继续加载