• Pytest----如何通过filterwarnings配置不显示告警或将告警报错


    【原文链接】Pytest----如何通过filterwarnings配置不显示告警或将告警报错

    首先在脚本中人工抛出一条告警,测试代码如下所示:

    import pytest
    import warnings
    
    def test_demo1():
        print("in test_demo1 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    执行结果如下所示,即这里在执行结果中也显示了一条告警信息。

    (demo-HCIhX0Hq) E:\demo>pytest -s
    =================== test session starts ===================
    platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    rootdir: E:\demo
    plugins: assume-2.4.3, rerunfailures-10.2
    collected 1 item
    
    test_demo.py in test_demo1 ...
    .
    
    ==================== warnings summary =====================
    test_demo.py::test_demo1
      E:\demo\test_demo.py:7: SyntaxWarning: warning,used to test...
        warnings.warn(SyntaxWarning("warning,used to test..."))
    
    -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
    ============== 1 passed, 1 warning in 0.02s ===============
    
    (demo-HCIhX0Hq) E:\demo>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在脚本中可以通过filterwarnings来配置告警信息的处理方式,比如将其忽略不显示,则代码如下,即此时在测试函数中加一个装饰器装饰,在装饰器中使用ignore,注意当按照告警类型忽略不显示的时候,ignore后面是两个冒号。

    import pytest
    import warnings
    
    @pytest.mark.filterwarnings("ignore::SyntaxWarning")
    def test_demo1():
        print("in test_demo1 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    执行结果如下,即不再有告警信息。

    (demo-HCIhX0Hq) E:\demo>pytest -s
    =================== test session starts ===================
    platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    rootdir: E:\demo
    plugins: assume-2.4.3, rerunfailures-10.2
    collected 1 item
    
    test_demo.py in test_demo1 ...
    .
    
    ==================== 1 passed in 0.02s ====================
    
    (demo-HCIhX0Hq) E:\demo>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    同理,若将告警转换为报错,则只需要将代码中的装饰器中的ignore修改为error即可,如下:

    import pytest
    import warnings
    
    @pytest.mark.filterwarnings("error::SyntaxWarning")
    def test_demo1():
        print("in test_demo1 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    执行结果如下,可以看到此时用例失败了,原因就是这个告警。

    (demo-HCIhX0Hq) E:\demo>pytest -s
    =================== test session starts ===================
    platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    rootdir: E:\demo
    plugins: assume-2.4.3, rerunfailures-10.2
    collected 1 item
    
    test_demo.py in test_demo1 ...
    F
    
    ======================== FAILURES =========================
    _______________________ test_demo1 ________________________
    
        @pytest.mark.filterwarnings("error::SyntaxWarning")
        def test_demo1():
            print("in test_demo1 ...")
    >       warnings.warn(SyntaxWarning("warning,used to test..."))
    E       SyntaxWarning: warning,used to test...
    
    test_demo.py:7: SyntaxWarning
    ================= short test summary info =================
    FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
    ==================== 1 failed in 0.07s ====================
    
    (demo-HCIhX0Hq) E:\demo>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    除此以外,还可以通过对告警信息进行正则匹配,比如当使用ignore时,此时ignore后面只跟一个冒号,冒号后面的即对告警信息的正则表达式,比如如下代码,即test_demo1中匹配告警信息中有test字符的就忽略,而test_demo2中则匹配告警信息中有demo字符的忽略,从代码分析,显然test_demo2中的告警信息无法匹配,即按照理论分析,test_demo1中的告警会忽略不显示,而test_demo2中的告警则会显示。

    import pytest
    import warnings
    
    @pytest.mark.filterwarnings("ignore:.*test.*")
    def test_demo1():
        print("in test_demo1 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
        
    @pytest.mark.filterwarnings("ignore:.*demo.*")
    def test_demo2():
        print("in test_demo2 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    执行结果如下,执行结果确实如此,即test_demo2中的告警继续报出来了,而test_demo1中的告警并未显示。

    (demo-HCIhX0Hq) E:\demo>pytest -s
    =================== test session starts ===================
    platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    rootdir: E:\demo
    plugins: assume-2.4.3, rerunfailures-10.2
    collected 2 items
    
    test_demo.py in test_demo1 ...
    .in test_demo2 ...
    .
    
    ==================== warnings summary =====================
    test_demo.py::test_demo2
      E:\demo\test_demo.py:13: SyntaxWarning: warning,used to test...
        warnings.warn(SyntaxWarning("warning,used to test..."))
    
    -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
    ============== 2 passed, 1 warning in 0.02s ===============
    
    (demo-HCIhX0Hq) E:\demo>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    同理,将告警转换为报错信息,也是同样支持正则表达式的,比如如下代码:

    import pytest
    import warnings
    
    @pytest.mark.filterwarnings("error:.*test.*")
    def test_demo1():
        print("in test_demo1 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    @pytest.mark.filterwarnings("error:.*demo.*")
    def test_demo2():
        print("in test_demo2 ...")
        warnings.warn(SyntaxWarning("warning,used to test..."))
        assert 1==1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    执行结果如下,显然test_demo1中的告警与正则表达匹配,因此报错了,而test_demo2中的告警并未匹配到正则表达式,因此不会报错,所以继续报出了告警信息。

    (demo-HCIhX0Hq) E:\demo>pytest -s
    =================== test session starts ===================
    platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    rootdir: E:\demo
    plugins: assume-2.4.3, rerunfailures-10.2
    collected 2 items
    
    test_demo.py in test_demo1 ...
    Fin test_demo2 ...
    .
    
    ======================== FAILURES =========================
    _______________________ test_demo1 ________________________
    
        @pytest.mark.filterwarnings("error:.*test.*")
        def test_demo1():
            print("in test_demo1 ...")
    >       warnings.warn(SyntaxWarning("warning,used to test..."))
    E       SyntaxWarning: warning,used to test...
    
    test_demo.py:7: SyntaxWarning
    ==================== warnings summary =====================
    test_demo.py::test_demo2
      E:\demo\test_demo.py:13: SyntaxWarning: warning,used to test...
        warnings.warn(SyntaxWarning("warning,used to test..."))
    
    -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
    ================= short test summary info =================
    FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
    ========= 1 failed, 1 passed, 1 warning in 0.07s ==========
    
    (demo-HCIhX0Hq) E:\demo>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    我想告诉你这样做就能做一个简单的个人网站出来:::全流程讲解(阿里云)
    mysql悲观锁是行锁还是表锁?
    Vue 之 插件 轮播组件 vue-awesome-swiper 的简单使用整理
    Docker原理详细剖析-Namespace
    Android面试题汇总(三)
    分布式数据库难题(二):数据复制
    【HttpRunner】接口自动化测试框架
    用前端写桌面应用
    Web安全漏洞解决方案
    UnionFind(并查集)
  • 原文地址:https://blog.csdn.net/redrose2100/article/details/128162313