pytest学习和使用19-pytest断言失败后,怎样保持后续的断言继续执行?(pytest-assume)

举报
虫无涯 发表于 2023/03/17 13:07:29 2023/03/17
【摘要】 1 引入pytest使用assert进行断言,如果有多个断言,第一个失败了,那么后续的断言将不会执行;那么如果第一个断言失败了,还想继续保持执行后边的断言,那如何做?我们可以使用pytest-assume来解决这个问题,即使用多重断言插件。 2 pytest-assume安装pip3 install pytest-assumeC:\Users\Administrator>pip3 inst...

1 引入

  • pytest使用assert进行断言,如果有多个断言,第一个失败了,那么后续的断言将不会执行;
  • 那么如果第一个断言失败了,还想继续保持执行后边的断言,那如何做?
  • 我们可以使用pytest-assume来解决这个问题,即使用多重断言插件。

2 pytest-assume安装

pip3 install pytest-assume
C:\Users\Administrator>pip3 install pytest-assume
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pytest-assume
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/5d/ecd54af3d3dc3f69e9cf527449a51bd5ca39cd47aaeab9a74e55603a9ce1/pytest_assume-2.4.3-py3-none-any.whl (6.0 kB)
Requirement already satisfied: pytest>=2.7 in d:\python37\lib\site-packages (from pytest-assume) (6.2.4)
Requirement already satisfied: six in d:\python37\lib\site-packages (from pytest-assume) (1.15.0)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (2.1.1)
Requirement already satisfied: atomicwrites>=1.0 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.4.0)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.10.2)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.13.1)
Requirement already satisfied: py>=1.8.2 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.10.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.4.4)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.1.1)
Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (20.8)
Requirement already satisfied: attrs>=19.2.0 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (20.3.0)
Requirement already satisfied: zipp>=0.5 in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=2.7->pytest-assume) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python37\lib\site-packages (from packaging->pytest>=2.7->pytest-assume) (2.4.7)
Installing collected packages: pytest-assume
Successfully installed pytest-assume-2.4.3

3 assert断言

  • 先看pytest自带的assert断言效果:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/16 
# 文件名称:test_assume.py
# 作用:pytest-assume插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

def test_case():
    a = 100
    b = 200
    assert a + b < 100
    assert a - b > 0
    assert a * b == 20000
    assert a / b == 200
    assert (b - a) / a == 1


if __name__ == '__main__':
    pytest.main(["-s", "test_assume.py"])

  • 运行结果如下,可以看到第一个断言失败后,后边的断言是无法进行的:
collecting ... collected 1 item

test_assume.py::test_case FAILED                                         [100%]
test_case\test_j\test_assume.py:8 (test_case)
def test_case():
        a = 100
        b = 200
>       assert a + b < 100
E       assert (100 + 200) < 100

test_assume.py:12: AssertionError

断言失败

断言失败


================================== FAILURES ===================================
__________________________________ test_case __________________________________

    def test_case():
        a = 100
        b = 200
>       assert a + b < 100
E       assert (100 + 200) < 100

test_assume.py:12: AssertionError
=========================== short test summary info ===========================
FAILED test_assume.py::test_case - assert (100 + 200) < 100
============================== 1 failed in 0.15s ==============================

4 pytest-assume断言

  • 代码如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/16 
# 文件名称:test_assume.py
# 作用:pytest-assume插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest


# def test_case():
#     a = 100
#     b = 200
#     assert a + b < 100
#     assert a - b > 0
#     assert a * b == 20000
#     assert a / b == 200
#     assert (b - a) / a == 1


def test_case01():
    a = 100
    b = 200
    pytest.assume(a + b < 100)
    pytest.assume(a - b > 0)
    pytest.assume(a * b == 20000)
    pytest.assume(a / b == 200)
    pytest.assume((b - a) / a == 1)
    print("执行到这了~~~~~~~~~~")


if __name__ == '__main__':
    pytest.main(["-s", "test_assume.py"])

  • 执行结果如下,可以看到前边有断言失败的,后边的断言是继续进行的:
test_assume.py::test_case01 FAILED                                       [100%]执行到这了~~~~~~~~~~

test_case\test_j\test_assume.py:21 (test_case01)
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None

    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               3 Failed Assumptions:
E               
E               test_assume.py:25: AssumptionFailure
E               >>	pytest.assume(a + b < 100)
E               AssertionError: assert False
E               
E               test_assume.py:26: AssumptionFailure
E               >>	pytest.assume(a - b > 0)
E               AssertionError: assert False
E               
E               test_assume.py:28: AssumptionFailure
E               >>	pytest.assume(a / b == 200)
E               AssertionError: assert False

D:\Python37\lib\site-packages\six.py:702: FailedAssumption

断言失败

断言失败


================================== FAILURES ===================================
_________________________________ test_case01 _________________________________

tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None

    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               3 Failed Assumptions:
E               
E               test_assume.py:25: AssumptionFailure
E               >>	pytest.assume(a + b < 100)
E               AssertionError: assert False
E               
E               test_assume.py:26: AssumptionFailure
E               >>	pytest.assume(a - b > 0)
E               AssertionError: assert False
E               
E               test_assume.py:28: AssumptionFailure
E               >>	pytest.assume(a / b == 200)
E               AssertionError: assert False

D:\Python37\lib\site-packages\six.py:702: FailedAssumption
---------------------------- Captured stdout call -----------------------------
执行到这了~~~~~~~~~~
=========================== short test summary info ===========================
FAILED test_assume.py::test_case01 - pytest_assume.plugin.FailedAssumption: 
============================== 1 failed in 0.17s ==============================
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。