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