Pytest----如何对产生的告警进行断言

【原文链接】Pytest----如何对产生的告警进行断言

对告警进行断言,可以通过with语句对告警的类型进行断言,如下,即断言代码中会产生UserWarning类型的告警。

import pytest
import warnings


def test_demo():
    print("in test_demo ...")
    with pytest.warns(UserWarning):
        warnings.warn(UserWarning("warning,used to demo..."))

执行结果如下,即断言通过。

(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo ...
.

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

(demo-HCIhX0Hq) E:\demo>

将测试代码修改为如下代码,即在代码中抛出SyntaxWarning类型的告警,而断言期望的是UserWarning类型的告警。

import pytest
import warnings


def test_demo():
    print("in test_demo ...")
    with pytest.warns(UserWarning):
        warnings.warn(SyntaxWarning("warning,used to demo..."))

执行结果如下,即此时报错了,显示并未有UserWarning类型的告警产生。

(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo ...
F

======================== FAILURES =========================
________________________ test_demo ________________________

    def test_demo():
        print("in test_demo ...")
        with pytest.warns(UserWarning):
>           warnings.warn(SyntaxWarning("warning,used to demo..."))
E           Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) were emitted.
E           The list of emitted warnings is: [SyntaxWarning('warning,used to demo...')].

test_demo.py:8: Failed
================= short test summary info =================
FAILED test_demo.py::test_demo - Failed: DID NOT WARN. No warnings of type (<class 'User...
==================== 1 failed in 0.07s ====================

(demo-HCIhX0Hq) E:\demo>

此外,还可以通过正则表达式对告警内容进行断言,如下即判断告警内容中含有demo。

import pytest
import warnings


def test_demo():
    print("in test_demo ...")
    with pytest.warns(UserWarning,match=r'.*demo.*'):
        warnings.warn(UserWarning("warning,used to demo..."))

执行结果如下,即断言通过。

(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo ...
.

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

(demo-HCIhX0Hq) E:\demo>

初次以为,还可以对函数调用中的告警类型进行断言,如下即断言add函数中会产生UserWarning类型的告警,同时,add函数需要给定两个参数a和b。

import pytest
import warnings

def add(a,b):
    print("a:",a)
    print("b:",b)
    warnings.warn(UserWarning("warning,used to demo..."))

def test_demo():
    print("in test_demo ...")
    pytest.warns(UserWarning,add,10,20)

执行结果如下,可以看出,参数a和b能够正常传入,而且也做到了对函数中产生的告警类型的断言。

(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo ...
a: 10
b: 20
.

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

(demo-HCIhX0Hq) E:\demo>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用: pytest -s -v [demo.py](http://demo.py) --workers 1 --tests-per-worker 4。 引用: 利用pytest-parallel:1进程4线程运行 pytest -s -v demo.py --workers 1 --tests-per-worker 4。 引用: 发现新问题 当前版本的 pytest-parallel 和pytest-xdist 在python3.9上不兼容。 根据引用和引用,可以看出这是在使用pytest执行测试用例的命令。其中,`-s`参数表示输出详细日志信息,`-v`参数表示输出详细的测试结果信息,`--workers 1`参数表示使用一个进程来运行测试用例,`--tests-per-worker 4`参数表示每个进程运行4个测试用例。 然而,根据引用提到的问题,当前版本的pytest-parallel和pytest-xdist在Python 3.9上不兼容。这可能意味着在Python 3.9上无法使用`pytest-parallel`来实现多线程运行测试用例的功能。 至于`--=no`部分,根据提供的引用内容中没有相关信息,无法给出具体解释。请提供更多上下文或引用内容以获得更准确的回答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证](https://blog.csdn.net/Franciz777/article/details/120976872)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值