一般我们做自动化测试时,一个用例会写多个断言,当第一个断言失败后,后面的代码就不会执行了,于是我们引进了pytest-assume插件可以解决断言失败后继续断言的问题。
一、安装依赖包
pip install pytest-assume
二、使用assert进行断言
我们发现test_01用例中,第一个断言assert 0 == 1失败时,后面的代码不会再执行, print('执行test_01断言2')和assert 1 == 2未执行。
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_01(self):"""用例1"""print('执行test_01断言1')assert 0 == 1print('执行test_01断言2')assert 1 == 2def test_02(self):"""用例2"""print('执行test_02断言1')assert 3 == 3print('执行test_02断言2')assert 4 == 4if __name__ == '__main__':pytest.main(['-s', 'test_C_01.py'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/代码/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\代码\Test\test
plugins: assume-2.2.1
收集的测试用例:[<Function test_01>, <Function test_02>]
排序后收集的测试用例:[<Function test_01>, <Function test_02>]
collected 2 itemstest_C_01.py 执行test_01断言1
F执行test_02断言1
执行test_02断言2
.================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________self = <test.test_C_01.Test object at 0x0000018C74AAEF48>def test_01(self):"""用例1"""print('执行test_01断言1')
> assert 0 == 1
E assert 0 == 1test_C_01.py:11: AssertionError
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - assert 0 == 1
========================= 1 failed, 1 passed in 0.06s =========================Process finished with exit code 0
二、使用pytest.assume进行断言,用法pytest.assume(预期结果 == 实际结果)
我们发现test_01用例中,第一个断言pytest.assume(0 == 1)失败时,print('执行test_01断言2')和pytest.assume(1 == 2)仍执行了,说明同一个用例中使用pytest.assume进行断言,如果第一个断言失败了,后面的代码仍会继续执行。
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_01(self):"""用例1"""print('执行test_01断言1')pytest.assume(0 == 1)print('执行test_01断言2')pytest.assume(1 == 2)def test_02(self):"""用例2"""print('执行test_02断言1')pytest.assume(3 == 3)print('执行test_02断言2')pytest.assume(4 == 4)if __name__ == '__main__':pytest.main(['-s', 'test_C_01.py'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/代码/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\代码\Test\test
plugins: assume-2.2.1
收集的测试用例:[<Function test_01>, <Function test_02>]
排序后收集的测试用例:[<Function test_01>, <Function test_02>]
collected 2 itemstest_C_01.py 执行test_01断言1
执行test_01断言2
F执行test_02断言1
执行test_02断言2
.================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef 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 2 Failed Assumptions:
E
E test_C_01.py:12: AssumptionFailure
E >> pytest.assume(0 == 1)
E AssertionError: assert False
E
E test_C_01.py:15: AssumptionFailure
E >> pytest.assume(1 == 2)
E AssertionError: assert False..\..\..\..\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - pytest_assume.plugin.FailedAssumption:
========================= 1 failed, 1 passed in 0.20s =========================Process finished with exit code 0