pytest-捕获告警信息

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

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

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

def test_one():
    assert api_v1() == 1

返回结果:

D:\pytest\exercise\chapter8>pytest -q test_show_warning.py
.                                                                                                                                                   [100%]
==================================================================== warnings summary ====================================================================
test_show_warning.py::test_one
  D:\pytest\exercise\chapter8\test_show_warning.py:7: UserWarning: 请使用新版本的api
    warnings.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 item

test_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() == 1

test_show_warning.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

   def api_v1():
>       warnings.warn(UserWarning('请使用新版本的api'))
E       UserWarning: 请使用新版本的api

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值