1. 话不多说,先上代码:
import warnings
import pytest
def api_v1():
warnings.warn(UserWarning("api v1, should use functions from v2"))
return 1
# @pytest.mark.filterwarnings("ignore:api v1")
def test_one():
assert api_v1() == 1
- 进入用例代码所在目录执行
pytest
,可以发现有warnings产生 - 有些warnings我们可能认为是不需要的,那么可以禁用掉,将在第2部分介绍
- 有些warnings我们希望升级为Error,将在第三部分介绍
(venv) C:\测试文件夹\project\python\pytest_demo\warning>pytest
=========================================================================================== test session starts ============================================================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\测试文件夹\project\python\pytest_demo\warning
collected 3 items
test_warning.py ... [100%]
============================================================================================= warnings summary =============================================================================================
test_warning.py::test_one
test_warning.py::test_two
test_warning.py::test_three
C:\测试文件夹\project\python\pytest_demo\warning\test_warning.py:6: UserWarning: api v1, should use functions from v2
warnings.warn(UserWarning("api v1, should use functions from v2"))
-- Docs: https://docs.pytest.org/en/stable/warnings.html
====================================================================================== 3 passed, 3 warnings in 0.02s =======================================================================================
2. 禁用warnings的方式
2.1 命令行处理方式
# 禁用所有warnings,通常不使用,禁用范围过大
pytest --disable-warnings
# 使用-W选项忽略warning,:: 表示具体的警告类名称、:表示匹配告警中msg的子串
pytest -W ignore::UserWarning
pytest -W "ignore:api v1"
- 执行结果如下
(venv) C:\测试文件夹\project\python\pytest_demo\warning>pytest --disable-warnings
=========================================== test session starts ============================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\测试文件夹\project\python\pytest_demo\warning
collected 3 items
test_warning.py ... [100%]
====================================== 3 passed, 3 warnings in 0.02s =======================================
2.2 使用装饰器@pytest.mark.filterwarnings
- 放开代码注释的装饰器,再次执行
pytest
(venv) C:\测试文件夹\project\python\pytest_demo\warning>pytest
=========================================== test session starts ============================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\测试文件夹\project\python\pytest_demo\warning
collected 1 item
test_warning.py . [100%]
============================================ 1 passed in 0.01s =============================================
- 装饰器过滤有两种写法
# : 表示匹配告警信息中的msg的子串
@pytest.mark.filterwarnings("ignore:api v1")
# :: 表示匹配告警信息中的告警类的类名,例如给出代码示例中的告警即可使用如下方式过滤告警
@pytest.mark.filterwarnings("ignore::UserWarning")
2.3 在pytest.ini
中进行告警过滤,该文件需要存放到项目根目录
- 需要注意的是,如果需要过滤非系统自带的warning,则需要指定warning的完整路径,如下文的InsecureRequestWarning
; content of pytest.ini
[pytest]
filterwarnings =
ignore::UserWarning
ignore::urllib3.exceptions.InsecureRequestWarning
2.4 禁用warnings插件方式
- 不建议直接使用,避免所有的warning都被禁用
- 在
pytest.ini
中添加
; content of pytest.ini
[pytest]
addopts = -p no:warnings
- 直接在命令行使用
pytest -p no:warnings
装饰器标记的过滤器优先于通过命令行传递或由pytest.ini
选项配置的过滤器
3. 升级warning
为Error
的方式
3.1 命令行处理方式
# 使用-W选项忽略warning,:: 表示具体的警告类名称、:表示匹配告警中msg的子串
pytest -W error::UserWarning
pytest -W "error:api v1"
- 执行结果如下
(venv) C:\测试文件夹\project\python\pytest_demo\warning> pytest -W "error::UserWarning"
=============================================================== test session starts ===============================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\测试文件夹\project\python\pytest_demo\warning
collected 1 item
test_warning.py F [100%]
==================================================================== FAILURES =====================================================================
____________________________________________________________________ test_one _____________________________________________________________________
def test_one():
> assert api_v1() == 1
test_warning.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def api_v1():
> warnings.warn(UserWarning("api v1, should use functions from v2"))
E UserWarning: api v1, should use functions from v2
test_warning.py:6: UserWarning
============================================================= short test summary info =============================================================
FAILED test_warning.py::test_one - UserWarning: api v1, should use functions from v2
================================================================ 1 failed in 0.12s ================================================================
(venv) C:\测试文件夹\project\python\pytest_demo\warning>pytest -W "error:api v1"
=============================================================== test session starts ===============================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\测试文件夹\project\python\pytest_demo\warning
collected 1 item
test_warning.py F [100%]
==================================================================== FAILURES =====================================================================
____________________________________________________________________ test_one _____________________________________________________________________
def test_one():
> assert api_v1() == 1
test_warning.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def api_v1():
> warnings.warn(UserWarning("api v1, should use functions from v2"))
E UserWarning: api v1, should use functions from v2
test_warning.py:6: UserWarning
============================================================= short test summary info =============================================================
FAILED test_warning.py::test_one - UserWarning: api v1, should use functions from v2
================================================================ 1 failed in 0.11s ================================================================
3.2 使用装饰器@pytest.mark.filterwarnings
- 装饰器过滤有两种写法,更换示例代码中的注释器内容为下述中任一个,可以完成告警升级
# : 表示匹配告警信息中的msg的子串
@pytest.mark.filterwarnings("error:api v1")
# :: 表示匹配告警信息中的告警类的类名,例如给出代码示例中的告警即可使用如下方式升级告警
@pytest.mark.filterwarnings("error::UserWarning")
3.3 在pytest.ini
中进行告警过滤,该文件需要存放到项目根目录
[pytest]
filterwarnings =
error::UserWarning
还可以在配置中直接标注 error
或者 ignore
表示匹配所有告警
如下示例即表示除UserWarning
忽略外,其余全部升级为error
# pytest.ini
[pytest]
filterwarnings =
error
ignore::UserWarning