pytest框架中有自带的断言assert ,但此断言在失败后后面的代码就不会继续运行了
例如:
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.8.0, pluggy-0.13.1
rootdir: D:httpRun
plugins: assume-2.2.1, allure-pytest-2.8.6, html-1.19.0, metadata-1.8.0
collected 2 items
test_debug.py F.
====================================================== FAILURES =======================================================
________________________________________________________ test _________________________________________________________
def test():
#pytest.assume(1==2)
> assert 1==2
E assert 1 == 2
test_debug.py:39: AssertionError
========================================= 1 failed, 1 passed in 0.23 seconds ==
在此段代码中,test函数中当assert 1==2 这段断言失败后,就不会在继续去断言 assert 1==1 这个条件,那么在用例中肯定不能以一条断言来进行验证
所以 就需要多条断言都去验证数据的准确性。
所以 此篇介绍下 pytest-assume 这个断言方式。
首先,安装pytest-assume的依赖包: pip install pytest-assume
安装后再pycharm中使用方法:
from pytest import assume
import pytest
def test():
pytest.assume(1==2)
pytest.assume(1==3)
pytest.assume(1==1)
运行后:
tp = <class 'pytest_assume.plugin.FailedAssumption'>
value = FailedAssumption('
2 Failed Assumptions:
test_debug.py:40: AssumptionFailure
>> pytest.assume(1==2)
AssertionError: assert False
test_debug.py:41: AssumptionFailure
>> pytest.assume(1==3)
AssertionError: assert False
',)
tb = <traceback object at 0x000001909B7C4C88>
def reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test_debug.py:40: AssumptionFailure
E >> pytest.assume(1==2)
E AssertionError: assert False
E
E test_debug.py:41: AssumptionFailure
E >> pytest.assume(1==3)
E AssertionError: assert False
c:program filespython36libsite-packagessix.py:685: FailedAssumption
========================================= 1 failed, 1 passed in 0.29 seconds
在pytest.assume(1==2) 断言失败后,代码依然会继续向下运行,直到代码运行完毕