1.告警信息的默认捕获行为

pytest可以自动捕获测试中产生的告警信息,在测试结束后进行展示

import warnings
def api_v1():warnings.warn(UserWarning('请使用新版本的api'))return 1def test_one():assert api_v1() == 1

返回结果:

D:\pytest\exercise\chapter8>pytest -q test_show_warning.py
.                                                                                                                                                   [100%]
==================================================================== warnings summary ====================================================================
test_show_warning.py::test_oneD:\pytest\exercise\chapter8\test_show_warning.py:7: UserWarning: 请使用新版本的apiwarnings.warn(UserWarning('请使用新版本的api'))-- Docs: https://docs.pytest.org/en/stable/warnings.html
1 passed, 1 warning in 0.02s

可以通过-W arg、命令行选项来自定义告警的捕获:
arg参数的格式为:

action:message:catagory:module:lineno;
  • action:只能在error、ignore、always(all)、default、module、once中取值,默认取值为default
  • category必须是warning的子类,默认取值为Warning类,表示所有的告警
  • module必须为字符串,表示特定模块产生的告警信息

一些常见的使用场景:

  • 忽略某一种类型的告警信息,例如会略UserWarning类型的告警(-W ignore::UserWarning)
D:\pytest\exercise\chapter8>pytest -W ignore::UserWarning test_show_warning.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8
plugins: allure-pytest-2.8.36
collected 1 itemtest_show_warning.py .                                                                                                                            [100%]================================================================== 1 passed in 0.07s ============================================================
  • 将一种类型的告警转换为异常来处理,例如:将UserWaarning告警转换为异常处理(-W error::Userwarning)
D:\pytest\exercise\chapter8>pytest -W error::UserWarning test_show_warning.py
================================================================== test session starts ===================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8
plugins: allure-pytest-2.8.36
collected 1 item                                                                                                                                          test_show_warning.py F                                                                                                                              [100%]======================================================================== FAILURES ========================================================================
________________________________________________________________________ test_one ________________________________________________________________________def test_one():
>       assert api_v1() == 1test_show_warning.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _def api_v1():
>       warnings.warn(UserWarning('请使用新版本的api'))
E       UserWarning: 请使用新版本的apitest_show_warning.py:7: UserWarning
================================================================ short test summary info =================================================================
FAILED test_show_warning.py::test_one - UserWarning: 请使用新版本的api
=================================================================== 1 failed in 0.11s ====================================================================
  • 只展示某一个模块中产生的告警,例如:只展示test_show_warnings模块中产生的告警信息,忽略其他的告警(-W ignore -W default:::test_show_warnings)
D:\pytest\exercise>pytest -W ignore -W default:::test_show_warnings chapter8/
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise
plugins: allure-pytest-2.8.36
collected 1 item                                                                                                                                        chapter8\test_show_warning.py .                                                                                                                   [100%]================================================================== 1 passed in 0.03s ===================================================================

这里我们演示了多个-W选项的组合操作,优先级是从左到右依次递增的;这里如果将它们调换一下顺序(即-W default:::test_show_warnings -W ignore),因为-W ignore最后生效,覆盖掉之前的操作,最终的结果就是我们一个告警信息都没有捕获到;

  • 也可以通过在pytest.ini文件中配置filterwarnings选项,来实现同样的操作。
[pytest]
filterwarnings =ignoredefault:::test_show_warnings

2. @pytest.mark.filterwarnings

上述操作是通过命令行执行的,使用起来多有不便,如何能够在用例、类、模块级别上自定义告警的捕获行为?可通过为测试项添加告警过滤器实现。
pytest.ini中定义的配置,禁止了除了test_show_warning模块外,其他的所有告警捕获行为。
现在新增用例, 禁止捕获由它所触发的用户告警:

@pytest.mark.filterwarnings('ignore::UserWarning')
def test_two():assert api_v1() ==1

测试结果:

D:\pytest\exercise\chapter8>pytest -k "test_two"
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 2 items / 1 deselected / 1 selected                                                                                                           test_show_warning.py .                                                                                                                            [100%]=========================================================== 1 passed, 1 deselected in 0.05s ============================================================

测试test_one:

D:\pytest\exercise\chapter8>pytest -k "test_one"
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 2 items / 1 deselected / 1 selected                                                                                                           test_show_warning.py .                                                                                                                            [100%]=========================================================== 1 passed, 1 deselected in 0.03s ============================================================

我们可以通过将@pytest.mark.filterwarnings应用于测试类为类中每一个测试用例添加过滤器。也可以通过设置pytestmark变量为整个测试模块中所有的用例添加告警过滤器:

pytestmark = pytest.mark.filterwarnings("error")

3. 取消告警信息的展示

D:\pytest\exercise\chapter8>pytest -k "test_one" --disable-warnings
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 2 items / 1 deselected / 1 selected                                                                                                           test_show_warning.py .                                                                                                                            [100%]=========================================================== 1 passed, 1 deselected in 0.03s ============================================================

4. 不使用告警的捕获功能

上小节中只是不展示捕获到的告警信息,我们可以通过- p no::warnings命令禁止告警的捕获行为。

D:\pytest\exercise\chapter8>pytest -k "test_one" -p no:warnings test_show_warning.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 2 items / 1 deselected / 1 selected                                                                                                           test_show_warning.py .                                                                                                                            [100%]=========================================================== 1 passed, 1 deselected in 0.02s ============================================================

5. DeprecationWarning和PendingDeprecationWarning告警

pytest会默认捕获DeprecationWarning和PendingDeprecationWarning类型的告警。有时候,你并不需要这种行为,可以通过在pytest.ini添加配置;例如,忽略告警信息匹配".*U.*mode is deprecated"的DeprecationWarning告警:

[pytest]
filterwarnings =ignore:.*U.*mode is deprecated:DeprecationWarning

5.1 pytest.deprecated_call方法

可以通过deprecated_call方法确保一段代码触发DeprecationWarning或者PendingDeprecationWarning告警。

import warnings
import pytestdef api_call_v1():warnings.warn("v1版本已经弃用,请使用v2版本的api")def test_deprecation():assert pytest.deprecated_call(api_call_v1()) == 200

返回结果:

D:\pytest\exercise\chapter8>pytest test_deprecation.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 1 item                                                                                                                                        test_deprecation.py F                                                                                                                             [100%]======================================================================= FAILURES =======================================================================
___________________________________________________________________ test_deprecation ___________________________________________________________________def test_deprecation():
>       assert pytest.deprecated_call(api_call_v1()) == 200
E       assert WarningsChecker(record=True) == 200
E        +  where WarningsChecker(record=True) = <function deprecated_call at 0x0000018F59D06280>(None)
E        +    where <function deprecated_call at 0x0000018F59D06280> = pytest.deprecated_call
E        +    and   None = api_call_v1()test_deprecation.py:12: AssertionError
=============================================================== short test summary info ================================================================
FAILED test_deprecation.py::test_deprecation - assert WarningsChecker(record=True) == 200
================================================================== 1 failed in 0.09s ===================================================================

deprecated_call也支持上下文管理器的写法:

def test_deprecation2():with pytest.deprecated_call():assert api_call_v1() == 200

6. 编写触发期望告警的断言

可以使用pytest.warns()作为上下文管理器,编写一个触发期望告警的断言,和pytest.raises()的用法很接近。
deprecaed_call方法的源码:

# _pytest/warnings.pydef deprecated_call(func=None, *args, **kwargs):__tracebackhide__ = Trueif func is not None:args = (func,) + argsreturn warns((DeprecationWarning, PendingDeprecationWarning), *args, **kwargs)

可以看出,deprecated_call也是pytest_warns()的封装,区别在于其指定了具体期望的告警类型。

通过上小节的例子看一下pytest.warns()的用法:

  • 可以为其传递一个关键字参数match,判断捕获到的告警信息是否匹配正则表。
def test_deprecation3():with pytest.warns((DeprecationWarning,PendingDeprecationWarning),match=r'v1版本已废弃'):assert api_call_v1() == 200

测试结果:

D:\pytest\exercise\chapter8>pytest -k test_deprecation3 test_deprecation.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 3 items / 2 deselected / 1 selected                                                                                                           test_deprecation.py F                                                                                                                             [100%]======================================================================= FAILURES =======================================================================
__________________________________________________________________ test_deprecation3 ___________________________________________________________________def test_deprecation3():with pytest.warns((DeprecationWarning,PendingDeprecationWarning),match=r'v1版本已废弃'):
>           assert api_call_v1() == 200
E           assert None == 200
E            +  where None = api_call_v1()test_deprecation.py:20: AssertionError
=============================================================== short test summary info ================================================================
FAILED test_deprecation.py::test_deprecation3 - assert None == 200
=========================================================== 1 failed, 2 deselected in 0.12s ============================================================
  • 我们也可以直接传递可低啊用对象,表达式返回执行这个可调用对象的结果:
def test_deprecation4():assert pytest.warns((DeprecationWarning,PendingDeprecationWarning), api_call_v1,match=r'和 pytest.raises() 方法一样,这时 pytest 不再判断告警信息是否正确') == 200

测试结果:

D:\pytest\exercise\chapter8>pytest -k test_deprecation4 test_deprecation.py
================================================================== test session starts ===================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 4 items / 3 deselected / 1 selected                                                                                                             test_deprecation.py F                                                                                                                               [100%]======================================================================== FAILURES ========================================================================
___________________________________________________________________ test_deprecation4 ____________________________________________________________________def test_deprecation4():
>       assert pytest.warns((DeprecationWarning,PendingDeprecationWarning), api_call_v1,match=r'和 pytest.raises() 方法一样,这时 pytest 不再判断告警信息是否正确') == 200
E       Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>, <class 'PendingDeprecationWarning'>) was emitted. The list of emitted warn
ings is: [UserWarning('v1版本已经弃用,请使用v2版本的api')].test_deprecation.py:24: Failed
================================================================ short test summary info =================================================================
FAILED test_deprecation.py::test_deprecation4 - Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>, <class 'PendingDeprecationWarni...============================================================ 1 failed, 3 deselected in 0.10s =============================================================
  • pytest.warns()可以返回一个列表,包含所有捕获到的告警对象 (warnings.WarningMessage)
import re
def test_deprecation5():with pytest.warns((DeprecationWarning,PendingDeprecationWarning)) as records:assert api_call_v1() == 200assert len(records) == 1assert re.search(r'v1版本已废弃',records[0].message.args[0])

返回结果:

D:\pytest\exercise\chapter8>pytest -k test_deprecation5 test_deprecation.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 5 items / 4 deselected / 1 selected                                                                                                           test_deprecation.py F                                                                                                                             [100%]======================================================================= FAILURES =======================================================================
__________________________________________________________________ test_deprecation5 ___________________________________________________________________def test_deprecation5():with pytest.warns((DeprecationWarning,PendingDeprecationWarning)) as records:
>           assert api_call_v1() == 200
E           assert None == 200
E            +  where None = api_call_v1()test_deprecation.py:31: AssertionError
=============================================================== short test summary info ================================================================
FAILED test_deprecation.py::test_deprecation5 - assert None == 200
=========================================================== 1 failed, 4 deselected in 0.11s ============================================================

实际上返回的不是一个列表。只是实现了__getitem__()和__len__()方法的普通类,其内部本身有一个_list的私有属性用于存储所有的数据。

pytest.warns()的源码如下:

# _pytest/recwarn.pydef warns(expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],*args: Any,match: Optional[Union[str, "Pattern"]] = None,**kwargs: Any
) -> Union["WarningsChecker", Any]:__tracebackhide__ = Trueif not args:if kwargs:msg = "Unexpected keyword arguments passed to pytest.warns: "msg += ", ".join(sorted(kwargs))msg += "\nUse context-manager form instead?"raise TypeError(msg)return WarningsChecker(expected_warning, match_expr=match)else:func = args[0]if not callable(func):raise TypeError("{!r} object (type: {}) must be callable".format(func, type(func)))with WarningsChecker(expected_warning):return func(*args[1:], **kwargs)

6.2 自定义失败时的提示消息

def test_deprecation6():with pytest.warns(Warning) as records:rsp = api_call_v1()if not records:pytest.fail('期望apu_call_v1 触发告警,实际上没有')assert rsp == 200

测试结果:

D:\pytest\exercise\chapter8>pytest -k test_deprecation6 test_deprecation.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 6 items / 5 deselected / 1 selected                                                                                                           test_deprecation.py F                                                                                                                             [100%]======================================================================= FAILURES =======================================================================
__________________________________________________________________ test_deprecation6 ___________________________________________________________________def test_deprecation6():with pytest.warns(Warning) as records:rsp = api_call_v1()if not records:pytest.fail('期望apu_call_v1 触发告警,实际上没有')
>           assert rsp == 200
E           assert None == 200test_deprecation.py:41: AssertionError
=============================================================== short test summary info ================================================================
FAILED test_deprecation.py::test_deprecation6 - assert None == 200
=========================================================== 1 failed, 5 deselected in 0.09s ============================================================

7. recwarn fixture

recwarn是一个用例级别的fixture,可以用于记录用例产生的所有告警。

def test_reprecation7(recwarn):api_call_v1()assert len(recwarn) == 1w = recwarn.pop()  #不指定告警类型,默认弹出最先报出的告警信息assert issubclass(w.category,(DeprecationWarning,PendingDeprecationWarning))assert re.search(r'v1版本已废弃',w.message.args[0])

测试结果:

D:\pytest\exercise\chapter8>pytest -k test_deprecation7 test_deprecation.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter8, configfile: pytest.ini
plugins: allure-pytest-2.8.36
collected 7 items / 7 deselected                                                                                                                        ================================================================ 7 deselected in 0.03s =================================================================

pytest自定义的告警类型

pytest自定义的告警类型:

pytest-捕获告警信息相关推荐

  1. pytest告警信息的处理方法

    一.过滤不必要的告警信息 1.在测试用例前添加装饰器, @pytest.mark.filterwarnings 2.定义全局变量pytestmark, # turns all warnings int ...

  2. 突出告警信息(DBA_OUTSTANDING_ALERTS)

    2019独角兽企业重金招聘Python工程师标准>>> 突出告警信息(DBA_OUTSTANDING_ALERTS) <循序渐进Oracle:数据库管理.优化与备份恢复> ...

  3. 使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  4. 钉钉api 获取 accesstoken_python3自定义告警信息发送至钉钉群

    至从公司正式使用钉钉后,我们告警信息就慢慢从微信转到了钉钉上,首先为什么从微信转到钉钉呢? 1.微信告警需要获取企业微信APPID.APPSECRET.agentid. 2.添加告警接受人时需要在企业 ...

  5. Catlyst 6509告警信息--把trunk口配置成access

    Catlyst 6509告警信息--把trunk口配置成access. Catlyst 6509核心交换告警信息处理--把trunk口配置成access. 2008-12-28    (jj) 1.  ...

  6. 实战技巧:Python爬取OEM 12C上的告警信息并推送至微信企业号

    关注我们获得更多精彩 作者 | 徐美兰 ,邮政软件开发工程师,广东.湖南邮政金融数据中心任职多年,擅长Oracle数据库性能诊断与调优,对基于Python的数据分析与挖掘开发具有浓厚兴趣. 编者说明: ...

  7. Js捕获地址栏信息,根据地址栏信息来判断是否要显示页面中的信息

    问题描述:Js捕获地址栏信息,根据地址栏信息来判断是否要显示页面中的信息 解决方法:使用window.location.href来判断地址栏中是否有要显示页面中某一区域的标识 function sel ...

  8. 海康设备接入EasyCVR,出现告警信息缺失且不同步该如何解决?

    EasyCVR可支持设备通过国标GB28181.RTMP.RTSP/Onvif.海康SDK.大华SDK.Ehome等协议接入,对外可分发RTSP.RTMP.FLV.HLS.WebRTC等格式的视频流. ...

  9. zabbix结合qqmail发送告警信息

    当我们部署好zabbix监控,为监控主机添加各种监控项完毕之后,如果一个主机出了问题,我们怎么能在第一时间获得报警信息,然后及时处理? 可以让zabbix通过邮件的方式把告警信息发给我们 首先需要使z ...

最新文章

  1. android系统底层驱动多个物理按键上报同一个键值给app层,app层如何区分
  2. 深度学习入门篇--手把手教你用 TensorFlow 训练模型
  3. python1080p壁纸高清图片_Python爬取高清桌面壁纸(附源码),直接运行即可
  4. XSS(跨站脚本攻击)攻击与防御
  5. 8、play框架中持久层操作
  6. 【数据结构与算法】【算法思想】 A *搜索算法
  7. java中为什么设计包装类,Java 中为什么要设计包装类
  8. ie系列浏览器_2020下半年河北教师资格准考证打印只能用ie浏览器吗
  9. [ExtJS 6] SenchaCmd编译出错与解决
  10. LeetCode:62. 不同路径(python、c++)
  11. Qt/C++ 网易云api 在线播放音乐
  12. micro usb接口定义图
  13. python下视频的包_Python——爬取包图网图片和视频
  14. 凯恩帝k1000ti参数设置_凯恩帝KND数控K1000TI系统维修
  15. 2017 追梦 远方
  16. java 2048思路_浅谈2048
  17. Oracle取某个时间点前后的整半小时的时间点
  18. 腾讯地图api-基本用法总结
  19. 机器学习-SVM算法
  20. 不用USBASP芯片也可用USB,纯AVR实现USB通讯:AVRUSB

热门文章

  1. 我敢打赌你一定不知道的软件测试基础知识整理
  2. 各种手机的User-Agent
  3. uni-app uView UI框架 下载安装教程
  4. linux 脚本 等待,在shell脚本中实现无限等待
  5. Help Hanzo(区间素数筛)
  6. 简单合并word文档(转)
  7. 微带线microstrip和带状线stripline
  8. JavaScript之显示和隐藏图片
  9. Notepad++ 替换换行符
  10. 【项目管理工具】—— Microsoft Office Project 介绍