遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

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

  

结果:

 

 

【bak】

 

posted @   全栈测试笔记  阅读(175)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示