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

Pytest----关闭所有告警显示

武飞扬头像
redrose2100
帮助1

【原文链接】Pytest----如何关闭所有告警显示

通过命令行参数还可以实现关闭所有告警,比如如下代码,这里人工抛出告警。

import warnings

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 ...
.

==================== warnings summary =====================
test_demo.py::test_demo1
  E:\demo\test_demo.py:5: SyntaxWarning: warning,used to test...
    warnings.warn(SyntaxWarning("warning,used to test..."))

test_demo.py::test_demo2
  E:\demo\test_demo.py:10: UserWarning: warning,used to demo...
    warnings.warn(UserWarning("warning,used to demo..."))

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

(demo-HCIhX0Hq) E:\demo>

在命令行使用–disable-warnings 参数即可关闭告警显示,如下所示,这里虽然结果中还显示有2条搞定,但是在会回显中具体告警内容不再显示了。

(demo-HCIhX0Hq) E:\demo>pytest --disable-warnings
=================== 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 ..                                      [100%]

============== 2 passed, 2 warnings in 0.02s ==============

(demo-HCIhX0Hq) E:\demo>

通过在命令行中使用如下参数 -p no:warnings,则可以彻底关闭告警,如下所示,此时结果中根本不会有告警内容的显示以及告警结果的显示。

(demo-HCIhX0Hq) E:\demo>pytest -p no:warnings
=================== 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 ..                                      [100%]

==================== 2 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbiff
系列文章
更多 icon
同类精品
更多 icon
继续加载