pytest扫盲21--pytest-assume多重效验插件

1、目的:

  解决多个assert断言中,其中一个断言失败,后续断言不再继续执行的问题。

2、使用插件:

  pytest-assume

3、安装: 

pip install pytest-assume -i https://pypi.tuna.tsinghua.edu.cn/simple

  

4、assert 多重断言:

# File  : test_demo_18_assume.py
# IDE   : PyCharm

a = 6
b = 3
def test_calc_1():
    assert a / b == 2
    assert a + b == 8
    assert a * b == 18
    assert a - b == 2
    print('测试结束!')

  执行结果,遇到断言失败即终止测试,最后的 print 没有打印:

E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
test_demo_18_assume.py::test_calc_1
当前用例运行测试环境:http://xx.xx.xx.xx:xxxx
F
================================== FAILURES ===================================
_________________________________ test_calc_1 _________________________________

    def test_calc_1():
        assert a / b == 2
>       assert a + b == 8
E       assert (6 + 3) == 8

test_demo_18_assume.py:12: AssertionError
=========================== short test summary info ===========================
FAILED test_demo_18_assume.py::test_calc_1 - assert (6 + 3) == 8
1 failed in 0.13s

Process finished with exit code 0

5、assume 断言

import pytest

def test_calc_2():
    pytest.assume(a / b == 2)
    pytest.assume(a + b == 8)
    pytest.assume(a * b == 18)
    pytest.assume(a - b == 2)
    print('测试结束!')

  执行结果,后面断言即使失败,仍然继续执行后面的代码。

E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
test_demo_18_assume.py::test_calc_2
当前用例运行测试环境:http://xx.xx.xx.xx:xxxx
测试结束!
F
================================== FAILURES ===================================
_________________________________ test_calc_2 _________________________________

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_demo_18_assume.py:21: AssumptionFailure
E               >>    pytest.assume(a + b == 8)
E               AssertionError: assert False
E               
E               test_demo_18_assume.py:23: AssumptionFailure
E               >>    pytest.assume(a - b == 2)
E               AssertionError: assert False

....python38libsite-packagessix.py:702: FailedAssumption
=========================== short test summary info ===========================
FAILED test_demo_18_assume.py::test_calc_2 - pytest_assume.plugin.FailedAssum...
1 failed in 0.19s

Process finished with exit code 0
原文地址:https://www.cnblogs.com/xiaohuboke/p/13572905.html