(19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume))
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 ==============================
标签:断言,19,py,assert,assume,pytest,test,100
From: https://blog.51cto.com/NoamaNelson/6127121