pytest简易教程(23):pytest中配置过滤警告
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
关于警告
如果警告不重要,可以忽略,如果警告很重要,可以提升为异常。
实现一:配置过滤警告
1、命令行参数,pytest case\test_qzcsbj.py -vs -W error::UserWarning,表示将UserWarning警告转换为错误
2、pytest.ini配置文件
表示将UserWarning警告转换为错误,其它忽略
1 2 3 4 | [pytest] filterwarnings = ignore error::UserWarning |
无参数
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings def test_a(): print( "---test_a" ) assert fun()==1 def fun(): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) return 1 |
无-W参数
pytest case\test_qzcsbj.py -vs
error:将警告转换为错误
pytest case\test_qzcsbj.py -vs -W error::UserWarning
ignore:忽略所有警告
default:打印每个警告
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings def test_b(): print( "---test_b" ) warnings.warn(UserWarning( "自定义warning" )) assert 1==1 def test_a(): print( "---test_a" ) assert fun()==1 def fun(): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
--disable-warnings:不显示警告摘要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings def test_b(): print( "---test_b" ) warnings.warn(UserWarning( "自定义warning" )) assert 1==1 def test_a(): print( "---test_a" ) assert fun()==1 def fun(): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
实现二:装饰器(filterwarnings过滤)
我们可以使用@pytest.mark.filterwarnings向特定测试项添加警告筛选器,这样可以做到更细节的控制警告
函数、方法级过滤
调用fun会产生警告,但是可以设置忽略警告
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings import pytest @pytest.mark.filterwarnings( "ignore:.*自定义.*" ) def test_a(): print( "---test_a" ) assert fun()==1 def fun(): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
也可以这样写:描述警告过滤器的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings import pytest # @pytest.mark.filterwarnings("ignore:.*自定义.*") @pytest.mark.filterwarnings( "ignore::UserWarning" ) def test_a(): print( "---test_a" ) assert fun()==1 def fun(): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
类级过滤
忽略含有“自定义”的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings import pytest @pytest.mark.filterwarnings( "ignore:自定义" ) class Test01: def test_a(self): print( "---test_a" ) assert self.fun()==1 def fun(self): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
模块级过滤
下面只能是pytestmark,不能改为其它的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import warnings import pytest # pytestmark = pytest.mark.filterwarnings("ignore:自定义") pytestmark = pytest.mark.filterwarnings( "ignore" ) class Test01: def test_a(self): print( "---test_a" ) assert self.fun()==1 def fun(self): print( "---fun" ) warnings.warn(UserWarning( "自定义warning" )) return 1 |
结果:
__EOF__

本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容