🔥 交流讨论:欢迎加入我们一起学习!
🔥 资源分享:耗时200+小时精选的「软件测试」资料包
🔥 教程推荐:火遍全网的《软件测试》教程
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
做自动化测试时我们一般会一个用例写多个断言,而当第一个断言失败后,后面的代码就不会执行。这时我们可以引进pytest-assume
插件来解决这些问题。
一、安装
pip install pytest-assume
- 1
二、案例
在test_01
用例中,第一个断言assert 0 == 1
失败,后面的代码不会执行。
import pytest
class Test(object):
def test_01(self):
"""用例1"""
print('执行test_01断言1')
assert 0 == 1
print('执行test_01断言2')
assert 1 == 2
def test_02(self):
"""用例2"""
print('执行test_02断言1')
assert 3 == 3
print('执行test_02断言2')
assert 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 items
test_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 == 1
test_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
预期结果 == 实际结果。
在test_01
用例中,第一个断言pytest.assume(0 == 1)
失败后,print('执行test_01断言2')
和pytest.assume(1 == 2)
仍执行了。
说明同一用例中使用pytest.assume
进行断言,即便第一个失败了,后面的代码也会继续执行。.
import pytest
class 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 items
test_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 = 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 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
最后我邀请你进入我们的【软件测试学习交流群:785128166】, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路
作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:程序员二黑】自提!