最近准备废弃之前用metersphere做的接口自动化,转战pytest了,先来分享下最近接触到的一个插件:pytest-assume。

在使用这个插件之前,如果一个用例里面有多个断言的话,前面的断言失败了,就不会去执行后面的断言:

pytest多重断言插件-pytest-assume_用例

接下来,看看如何使用pytest-assume插件来实现多重断言。

插件安装:

`pip install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com`  
  or   
  `pip install pytest-assume`

使用demo:

import pytest




@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
    pytest.assume(x == y)
    pytest.assume(x > y)
    pytest.assume(x < y)

代码运行后,可以看到就算前面的断言失败了的话,后面的断言也会继续执行:

pytest多重断言插件-pytest-assume_python_02

另外一种等价的写法:

import pytest
from pytest import assume




@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
    with assume: assert x == y
    with assume: assert x > y
    with assume: assert x < y

注意:每一个断言要用一个with assume,如果在一个with assume下面写多个断言的话,前面的断言失败后,后面的断言就不会执行

import pytest




def test_simple_assume():
    a = -1
    b = 2
    c = 3


    with pytest.assume:
        assert a > 0
        assert b > 0
        assert c < 0

停了好久没更新了,最近会找回学习的状态,更多的笔记会记录到https://xiaobotester.readthedocs.io/ 博客中。