Pytest----如何捕获告警

简介: Pytest----如何捕获告警

一、使用-W参数可以设置告警不显示或者报错

test_demo.py代码如下

import warnings


def api_v1():
    warnings.warn(UserWarning("api v1, should use functions from v2"))
    return 1

def test_one():
    assert api_v1() == 1

使用pytest执行结果如下,这里在执行最后会显示告警

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

=========================================================================== warnings summary ===========================================================================
test_demo.py::test_one
  D:\src\blog\tests\test_demo.py:5: 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
===================================================================== 1 passed, 1 warning in 0.03s =====================================================================

(1)使用 -W 参数,加上error选项,可以将指定的告警转换为错误

$ pytest -W error::UserWarning
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py F                                                                                                                                                    [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_one _______________________________________________________________________________

    def test_one():
>       assert api_v1() == 1

test_demo.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def api_v1():
>       warnings.warn(UserWarning("api v1, should use functions from v2"))
E       UserWarning: api v1, should use functions from v2

test_demo.py:5: UserWarning
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2
========================================================================== 1 failed in 0.10s ===========================================================================

(2)使用 -W 参数,加上ignore选项,可以忽略指定的告警

$ pytest -W ignore::UserWarning
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.02s ===========================================================================

二、使用pytest.ini配置可以设置告警不显示或者报错

(1)可以在pytest.ini中配置,如下在pytest.ini中配置忽略指定的告警

pytest.ini内容如下

[pytest]
filterwarnings =
    error
    ignore::UserWarning

使用pytest执行结果:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.02s ===========================================================================

(2)如下,在pytest.ini中配置将指定告警转换为错误

pytest.ini内容如下

[pytest]
filterwarnings =
    ignore
    error::UserWarning

使用pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py F                                                                                                                                                    [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_one _______________________________________________________________________________

    def test_one():
>       assert api_v1() == 1

test_demo.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def api_v1():
>       warnings.warn(UserWarning("api v1, should use functions from v2"))
E       UserWarning: api v1, should use functions from v2

test_demo.py:5: UserWarning
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2
========================================================================== 1 failed in 0.10s ===========================================================================

(3)通过对告警内容正则匹配忽略告警

pytest.ini内容如下:

[pytest]
filterwarnings =
    ignore:.*?should use functions

pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.02s ===========================================================================

(4)通过对告警内容正则匹配报错

pytest.ini内容如下:

[pytest]
filterwarnings =
    error:.*?should use functions

pytest命令执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py F                                                                                                                                                    [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_one _______________________________________________________________________________

    def test_one():
>       assert api_v1() == 1

test_demo.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def api_v1():
>       warnings.warn(UserWarning("api v1, should use functions from v2"))
E       UserWarning: api v1, should use functions from v2

test_demo.py:5: UserWarning
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2
========================================================================== 1 failed in 0.10s ===========================================================================

三、使用@pytest.mark.filterwarnings可以设置告警不显示或者报错

test_demo.py代码如下:

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.mark.filterwarnings("ignore::UserWarning")
def test_two():
    assert api_v1() == 1


@pytest.mark.filterwarnings("error:api v1")
def test_three():
    assert api_v1() == 1


@pytest.mark.filterwarnings("error::UserWarning")
def test_four():
    assert api_v1() == 1

pytest命令执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 4 items                                                                                                                                                       

test_demo.py ..FF                                                                                                                                                 [100%]

=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_three ______________________________________________________________________________

    @pytest.mark.filterwarnings("error:api v1")
    def test_three():
>       assert api_v1() == 1

test_demo.py:21:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def api_v1():
>       warnings.warn(UserWarning("api v1, should use functions from v2"))
E       UserWarning: api v1, should use functions from v2

test_demo.py:6: UserWarning
______________________________________________________________________________ test_four _______________________________________________________________________________

    @pytest.mark.filterwarnings("error::UserWarning")
    def test_four():
>       assert api_v1() == 1

test_demo.py:26:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def api_v1():
>       warnings.warn(UserWarning("api v1, should use functions from v2"))
E       UserWarning: api v1, should use functions from v2

test_demo.py:6: UserWarning
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_three - UserWarning: api v1, should use functions from v2
FAILED test_demo.py::test_four - UserWarning: api v1, should use functions from v2
===================================================================== 2 failed, 2 passed in 0.12s ======================================================================

四、对一个测试文件中中的所有测试函数中产生的告警都转换为错误或者都忽略

只需要在文件开头声明一个pytestmark即可,如下

test_demo.py内容如下:

import warnings
import pytest

pytestmark = pytest.mark.filterwarnings("error")


@pytest.mark.filterwarnings("ignore:api v1")
def test_one():
    warnings.warn(UserWarning("user warning in test_one..."))
    assert 1 == 1

@pytest.mark.filterwarnings("ignore::UserWarning")
def test_two():
    warnings.warn(DeprecationWarning("deprecation warning in test_one..."))
    assert 1 == 1

pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py FF                                                                                                                                                   [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_one _______________________________________________________________________________

    @pytest.mark.filterwarnings("ignore:api v1")
    def test_one():
>       warnings.warn(UserWarning("user warning in test_one..."))
E       UserWarning: user warning in test_one...

test_demo.py:9: UserWarning
_______________________________________________________________________________ test_two _______________________________________________________________________________

    @pytest.mark.filterwarnings("ignore::UserWarning")
    def test_two():
>       warnings.warn(DeprecationWarning("deprecation warning in test_one..."))
E       DeprecationWarning: deprecation warning in test_one...

test_demo.py:14: DeprecationWarning
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_one - UserWarning: user warning in test_one...
FAILED test_demo.py::test_two - DeprecationWarning: deprecation warning in test_one...
========================================================================== 2 failed in 0.12s ===========================================================================

将test_demo.py中pytestmark中的error修改为ignore,即:

import warnings
import pytest

pytestmark = pytest.mark.filterwarnings("ignore")


@pytest.mark.filterwarnings("ignore:api v1")
def test_one():
    warnings.warn(UserWarning("user warning in test_one..."))
    assert 1 == 1

@pytest.mark.filterwarnings("ignore::UserWarning")
def test_two():
    warnings.warn(DeprecationWarning("deprecation warning in test_one..."))
    assert 1 == 1

此时pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py ..                                                                                                                                                   [100%]

========================================================================== 2 passed in 0.03s ===========================================================================

五、关闭所有告警

test_demo.py内容如下:

import warnings
import pytest


@pytest.mark.filterwarnings("ignore:api v1")
def test_one():
    warnings.warn(UserWarning("user warning in test_one..."))
    assert 1 == 1

@pytest.mark.filterwarnings("ignore::UserWarning")
def test_two():
    warnings.warn(DeprecationWarning("deprecation warning in test_one..."))
    assert 1 == 1

(1)使用--disable-warnings参数关闭告警详情
如:

$ pytest --disable-warnings
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py ..                                                                                                                                                   [100%]

==================================================================== 2 passed, 2 warnings in 0.03s =====================================================================

(2)在pytest.ini按照如下配置

[pytest]
addopts=--disable-warnings

执行pytest结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py ..                                                                                                                                                   [100%]

==================================================================== 2 passed, 2 warnings in 0.02s =====================================================================

(3)使用pytest -p no:warnings彻底屏蔽告警

执行如下:

$ pytest -p no:warnings
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py ..                                                                                                                                                   [100%]

========================================================================== 2 passed in 0.03s ===========================================================================

(4)通过pytest.ini配置如下配置,彻底屏蔽告警

[pytest]
addopts=-p no:warnings

此时执行pytest结果如下

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py ..                                                                                                                                                   [100%]

========================================================================== 2 passed in 0.02s ===========================================================================

六、对产生的告警进行断言

(1)对告警类型进行断言,test_demo.py代码如下

import warnings
import pytest


def test_warning():
    with pytest.warns(UserWarning):
        warnings.warn("my warning", UserWarning)

使用pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.03s ===========================================================================

(2)通过正则匹配对告警内容进行匹配,test_demo.py代码如下:

import warnings
import pytest


def test_warning():
    with pytest.warns(UserWarning, match=r'must be \d+$'):
        warnings.warn("value must be 42", UserWarning)

pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.02s ===========================================================================

(3)可以对一个函数中的产生的告警进行断言

test_demo.py代码如下:

import warnings
import pytest

def func_demo(a,b):
    warnings.warn("value must be 42", UserWarning)


def test_warning():
    pytest.warns(UserWarning,func_demo,10,20)

使用pytest执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py .                                                                                                                                                    [100%]

========================================================================== 1 passed in 0.02s ===========================================================================

七、记录测试函数中产生的告警的fixture:recwarn

如下将整个测试函数中产生的告警均记录下来可供解析,test_demo.py代码如下:

import warnings

def test_hello(recwarn):
    warnings.warn("hello", UserWarning)
    warnings.warn("world", UserWarning)
    assert len(recwarn) == 2
    w = recwarn.pop()
    print("---------------------------")
    print(w.category)
    print(w.message)
    print(w.filename)
    print(w.lineno)
    print("---------------------------")
    w = recwarn.pop()
    print("---------------------------")
    print(w.category)
    print(w.message)
    print(w.filename)
    print(w.lineno)
    print("---------------------------")

执行结果如下:

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo.py ---------------------------
<class 'UserWarning'>
hello
D:\src\blog\tests\test_demo.py
4
---------------------------
---------------------------
<class 'UserWarning'>
world
D:\src\blog\tests\test_demo.py
5
---------------------------
.

========================================================================== 1 passed in 0.03s ===========================================================================
目录
相关文章
|
测试技术
21-pytest-severity标记用例优先级
21-pytest-severity标记用例优先级
|
测试技术 C++ Python
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
|
测试技术
Pytest----如何管理日志
Pytest----如何管理日志
688 0
|
测试技术
Pytest----失败N次后停止执行
Pytest----失败N次后停止执行
366 0
|
开发工具
Pytest----生成allure执行报告
Pytest----生成allure执行报告
265 0
Pytest----生成allure执行报告
|
测试技术
Pytest----自动化脚本的加载原理
Pytest----自动化脚本的加载原理
477 1
|
测试技术
Pytest 系列(25)- @allure.severity 标记用例级别
Pytest 系列(25)- @allure.severity 标记用例级别
240 0
Pytest 系列(25)- @allure.severity 标记用例级别
|
测试技术
利用装饰器,帮你自动处理异常并优雅实现重跑case
利用装饰器,帮你自动处理异常并优雅实现重跑case
175 0
|
测试技术
Pytest----自定义Pytest断言报错信息
Pytest----自定义Pytest断言报错信息
385 0
|
测试技术
Pytest----如何正确使用pytest的日志功能
Pytest----如何正确使用pytest的日志功能
517 0