Pytest----命令行如何控制不显示告警或将告警报错

【原文链接】Pytest----命令行如何控制不显示告警或将告警报错

在执行自动化脚本的时候,出现告警的情况时非常常见的,比如我们使用了一个很快要被废弃了的语法,比如我们用了一个不被推荐的用法等等,当然可以对告警不做任何处理,但是从追求完美的角度或者说从学习技术的角度,我们还是有必要去了解一下有哪些方法来处理告警,首先看如下代码,这里使用了一个未声明的mark标签。

import pytest

@pytest.mark.smoke
def test_demo1():
    print("in test_demo1 ...")
    assert 1==1

执行结果如下,这里就产生了一个告警,提示smoke这个标签被注册,告警中还给出了参考文档的链接。

(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 1 item

test_demo.py in test_demo1 ...
.

==================== warnings summary =====================
test_demo.py:3
  E:\demo\test_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.smoke

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============== 1 passed, 1 warning in 0.02s ===============

(demo-HCIhX0Hq) E:\demo>

从执行的结果可以看出,告警信息给常多,不利于问题定位以及回显显示,所以在有些情况下我们可能希望不要显示这些告警信息,此时就可以使用 –W ignore 即可,如下所示。

(demo-HCIhX0Hq) E:\demo>pytest -W ignore
=================== 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 1 item

test_demo.py .                                       [100%]

==================== 1 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

当然从另外一个角度,有告警说明告警提示最好还是要做适当的修改调整的,那么如果在追求更加完美的角度,为了让脚本开发者必须解决告警的情况下,可以使用 –W error ,这样一来,所有的告警信息就会转换为报错,即强制使得测试用例失败,因为只有用例失败了,脚本开发者才会不得不去作出对应的修改和调整。
如下所示,这里就将测试smoke的标签显示为错误,这样一来脚本开发者就必须去解决这个问题。当然是否要采用这种策略,要根据具体的情况而定。

(demo-HCIhX0Hq) E:\demo>pytest -W error
=================== 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 0 items / 1 error

========================= ERRORS ==========================
______________ ERROR collecting test_demo.py ______________
test_demo.py:3: in <module>
    @pytest.mark.smoke
C:\Users\Administrator\.virtualenvs\demo-HCIhX0Hq\lib\site-packages\_pytest\mark\structures.py:549: in __getattr__
    2,
E   pytest.PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
================= short test summary info =================
ERROR test_demo.py - pytest.PytestUnknownMarkWarning: Unknown pytest.mark.sm...
!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!
==================== 1 error in 0.11s =====================

(demo-HCIhX0Hq) E:\demo>

上面的对告警的处理是将所有的告警统一处理,即当使用 –W ignore 时,所有类型的告警都将不显示,当使用 –W error 时,所有类型的告警都将报错,在实际应用中,我们可能会遇到这种场景,即希望一些特定类型的告警不显示,或者说希望特定类型的告警报错,此时就需要在 –W ignore 和 –W error 后面继续指定告警类型了。
比如如下测试代码,这里一种是smoke标签这种未注册类型的告警,一种是人为抛出的用户类型告警。

import pytest
import warnings

@pytest.mark.smoke
def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
assert 1==1

执行结果如下,这里显示有两个告警,而且从执行结果中可以到一个告警的类型是PytestUnknownMarkWarning, 另一个告警的类型是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 1 item

test_demo.py in test_demo1 ...
.

==================== warnings summary =====================
test_demo.py:4
  E:\demo\test_demo.py:4: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.smoke

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
============== 1 passed, 2 warnings in 0.02s ==============

(demo-HCIhX0Hq) E:\demo>

比如这里不想显示PytestUnknownMarkWarning,而希望SyntaxWarning告警正常显示,此时就需要指定告警类型进行精确不显示了。这里就涉及到Python中内置的告警类型了,首先在Python语言中Warning是所有告警的父类,也就是如果指定Warning类型,则所有告警将别忽略不显示,如下所示。

(demo-HCIhX0Hq) E:\demo>pytest -W ignore::Warning
=================== 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 1 item

test_demo.py .                                       [100%]

==================== 1 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

子类告警类型主要有BytesWarning,DeprecationWarning,FutureWarning,ImportWarning,PendingDeprecationWarning,ResourceWarning,RuntimeWarning,SyntaxWarning,UnicodeWarning,UserWarning,显然Python内置告警类型中没有PytestUnknownMarkWarning,那么这里如果直接指定PytestUnknownMarkWarning类型是会报错的,如下所示。

(demo-HCIhX0Hq) E:\demo>pytest -W ignore::PytestUnknownMarkWarning
ERROR: while parsing the following warning configuration:

  ignore::PytestUnknownMarkWarning

This error occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\.virtualenvs\demo-HCIhX0Hq\lib\site-packages\_pytest\config\__init__.py", line 1690, in parse_warning_filter
    category: Type[Warning] = _resolve_warning_category(category_)
  File "C:\Users\Administrator\.virtualenvs\demo-HCIhX0Hq\lib\site-packages\_pytest\config\__init__.py", line 1729, in _resolve_warning_category
    cat = getattr(m, klass)
AttributeError: module 'builtins' has no attribute 'PytestUnknownMarkWarning'



(demo-HCIhX0Hq) E:\demo>

实质上PytestUnknownMarkWarning是UserWarning类型的子类,因此这里只需要指定UserWarning类型即可,执行结果如下所示,即此时执行结果只显示脚本中主动抛出的SyntaxWarning的告警了。

(demo-HCIhX0Hq) E:\demo>pytest -W ignore::UserWarning
=================== 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 1 item

test_demo.py .                                       [100%]

==================== 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
============== 1 passed, 1 warning in 0.02s ===============

(demo-HCIhX0Hq) E:\demo>

同理,如果希望脚本中主动抛出的SyntaxWarning类型的告警为错误,同样的方式在-W error后指定即可,如下所示:

(demo-HCIhX0Hq) E:\demo>pytest -W error::SyntaxWarning
=================== 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 1 item

test_demo.py F                                       [100%]

======================== FAILURES =========================
_______________________ test_demo1 ________________________

    @pytest.mark.smoke
    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
------------------ Captured stdout call -------------------
in test_demo1 ...
==================== warnings summary =====================
test_demo.py:4
  E:\demo\test_demo.py:4: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.smoke

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================= short test summary info =================
FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
============== 1 failed, 1 warning in 0.07s ===============

(demo-HCIhX0Hq) E:\demo>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

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

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

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

打赏作者

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

抵扣说明:

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

余额充值