pytest的多重断言

2021-06-12  本文已影响0人  Chaweys

安装插件:
pip3 install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

import pytest
def test_01():
   assert 1 + 3 == 4
   assert 2 + 3 == 4
   assert 3 + 3 == 6
   assert 4 + 3 == 7
   print("测试完成")

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

"""
结果:由结果看出在断言失败assert 2 + 3 == 4之后,后面的代码不再执行
test_py01.py::test_01 FAILED                                             [100%]
order\test_py01.py:6 (test_01)
5 != 4

Expected :4
Actual   :5
<Click to see difference>

def test_01():
       assert 1 + 3 == 4
>       assert 2 + 3 == 4
E       assert 5 == 4

test_py01.py:9: AssertionError
"""


使用多重断言插件
import pytest
def test_add():
   pytest.assume(1 + 3 == 4)
   pytest.assume(2 + 3 == 4)
   pytest.assume(3 + 3 == 5)
   pytest.assume(4 + 3 == 5)
   print("测试完成")

if __name__ == '__main__':
   pytest.main(["-s","test_py01.py"])
   
"""
结果:由结果得出,在第一个断言失败后,后面的代码仍会执行
teamcity[testFailed timestamp='2021-06-03T17:09:42.152' details='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_py01.py:11: AssumptionFailure
E               >>  pytest.assume(2 + 3 == 4)
E               AssertionError: assert False
E               
E               test_py01.py:12: AssumptionFailure
E               >>  pytest.assume(3 + 3 == 5)
E               AssertionError: assert False
E               
E               test_py01.py:13: AssumptionFailure
E               >>  pytest.assume(4 + 3 == 5)
E               AssertionError: assert False
"""
上一篇 下一篇

猜你喜欢

热点阅读