多个断言连续执行pytest-assume && try except assert 错误思路

前言:

在使用多个断言时,第一个断言失败,程序会终止,不会执行后面的断言

思路一(正确):pytest-assume

1.pytest-assume

pip install pytest-assume

实例

import pytest

@pytest.mark.parametrize(('x', 'y'),
                         [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
    print("测试数据x=%s, y=%s" % (x, y))
    pytest.assume(x == y)
    pytest.assume(x+y > 1)
    pytest.assume(x > 1)
    print("测试完成!")

运行结果:上一个断言失败不会影响下一个断言的执行

上下文管理器

import pytest
from pytest import assume


@pytest.mark.parametrize(('x', 'y'),
                         [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
    print("测试数据x=%s, y=%s" % (x, y))
    with assume: assert x == y
    with assume: assert x+y > 1
    with assume: assert x > 1
    print("测试完成!")

 

思路二: 使用try except  assert

try:
    assert 1 == 2
assert 1 == 1
except: print('abudengb') try: assert a > b except: print('a不大于b')

这样是不对的,因为这样程序不会报错,allure测试报告为通过

 

posted @ 2020-06-17 14:18  躺云飘  阅读(995)  评论(0编辑  收藏  举报