遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】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警告转换为错误,其它忽略

[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

  

结果:

 

 

【bak】

 

posted @ 2024-02-23 22:15  全栈测试笔记  阅读(102)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end