前言:
在使用多個斷言時,第一個斷言失敗,程序會終止,不會執行后面的斷言
思路一(正確):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測試報告為通過
