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警告转换为错误,其它忽略
[pytest]
filterwarnings =
ignore
error::UserWarning
无参数
代码
#!/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:打印每个警告
#!/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:不显示警告摘要
#!/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会产生警告,但是可以设置忽略警告
#!/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
结果:

也可以这样写:描述警告过滤器的写法
#!/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
结果:
类级过滤
忽略含有“自定义”的
#!/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,不能改为其它的
#!/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
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!

浙公网安备 33010602011771号