pytest文檔36-斷言失敗后還能繼續執行pytest-assume


前言

pytest的斷言失敗后,后面的代碼就不會執行了,通常一個用例我們會寫多個斷言,有時候我們希望第一個斷言失敗后,后面能繼續斷言。
pytest-assume插件可以解決斷言失敗后繼續斷言的問題。github地址https://github.com/astraw38/pytest-assume

環境准備

先安裝pytest-assume依賴包

pip install pytest-assume

遇到問題

以下是一個簡單案例,輸入的測試數據有3種,我們需要斷言同時滿足三種情況

  • x == y
  • x+y > 1
  • x > 1
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))
    assert x == y
    assert x+y > 1
    assert x > 1

運行結果

D:\demo\test_yoyo.py:9: AssertionError


================================== FAILURES ===================================
___________________________ test_simple_assume[1-1] ___________________________

x = 1, y = 1

    @pytest.mark.parametrize(('x', 'y'),
                             [(1, 1), (1, 0), (0, 1)])
    def test_simple_assume(x, y):
        print("測試數據x=%s, y=%s" % (x, y))
        assert x == y
        assert x+y > 1
>       assert x > 1
E       assert 1 > 1

D:\soft\code\pytest_api_2020_03\demo\test_yoyo.py:11: AssertionError
___________________________ test_simple_assume[1-0] ___________________________

x = 1, y = 0

    @pytest.mark.parametrize(('x', 'y'),
                             [(1, 1), (1, 0), (0, 1)])
    def test_simple_assume(x, y):
        print("測試數據x=%s, y=%s" % (x, y))
>       assert x == y
E       assert 1 == 0

D:\soft\code\pytest_api_2020_03\demo\test_yoyo.py:9: AssertionError
___________________________ test_simple_assume[0-1] ___________________________

x = 0, y = 1

    @pytest.mark.parametrize(('x', 'y'),
                             [(1, 1), (1, 0), (0, 1)])
    def test_simple_assume(x, y):
        print("測試數據x=%s, y=%s" % (x, y))
>       assert x == y
E       assert 0 == 1

D:\demo\test_yoyo.py:9: AssertionError
========================== 3 failed in 0.26 seconds ===========================

如果第一個斷言就失敗了,后面的2個斷言都不會執行了

pytest-assume使用案例

使用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("測試完成!")

運行結果

================================== FAILURES ===================================
___________________________ test_simple_assume[1-1] ___________________________

tp = <class 'pytest_assume.plugin.FailedAssumption'>
value = FailedAssumption('\demo\\test_yoyo.py:11: AssumptionFailure\n>>\tpytest.assume(x > 1)\n
AssertionError: assert False\n\n',)
tb = <traceback object at 0x00000216CA579B08>

    def reraise(tp, value, tb=None):
        if value is None:
            value = tp()
        if value.__traceback__ is not tb:
>           raise value.with_traceback(tb)
E           pytest_assume.plugin.FailedAssumption: 
E           1 Failed Assumptions:
E           
E           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:11: AssumptionFailure
E           >>	pytest.assume(x > 1)
E           AssertionError: assert False

E:\python36\lib\site-packages\six.py:685: FailedAssumption
___________________________ test_simple_assume[1-0] ___________________________

tp = <class 'pytest_assume.plugin.FailedAssumption'>
value = FailedAssumption('\demo\\test_yoyo.py:9:...st_api_2020_03\\demo\\test_yoyo.py:11: 
AssumptionFailure\n>>\tpytest.assume(x > 1)\nAssertionError: assert False\n\n',)
tb = <traceback object at 0x00000216CA579448>

    def reraise(tp, value, tb=None):
        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           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:9: AssumptionFailure
E           >>	pytest.assume(x == y)
E           AssertionError: assert False
E           
E           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:10: AssumptionFailure
E           >>	pytest.assume(x+y > 1)
E           AssertionError: assert False
E           
E           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:11: AssumptionFailure
E           >>	pytest.assume(x > 1)
E           AssertionError: assert False

E:\python36\lib\site-packages\six.py:685: FailedAssumption
___________________________ test_simple_assume[0-1] ___________________________

tp = <class 'pytest_assume.plugin.FailedAssumption'>
value = FailedAssumption('\n3 Failed Assumptions:\demo\\test_yoyo.py:11:
 AssumptionFailure\n>>\tpytest.assume(x > 1)\nAssertionError: assert False\n\n',)
tb = <traceback object at 0x00000216CA74D2C8>

    def reraise(tp, value, tb=None):
        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           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:9: AssumptionFailure
E           >>	pytest.assume(x == y)
E           AssertionError: assert False
E           
E           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:10: AssumptionFailure
E           >>	pytest.assume(x+y > 1)
E           AssertionError: assert False
E           
E           ..\..\..\..\..\soft\code\pytest_api_2020_03\demo\test_yoyo.py:11: AssumptionFailure
E           >>	pytest.assume(x > 1)
E           AssertionError: assert False

E:\python36\lib\site-packages\six.py:685: FailedAssumption
========================== 3 failed in 0.44 seconds ===========================

從運行結果可以看出,三個斷言都會執行

上下文管理器

pytest.assume 也可以使用上下文管理器去斷言

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("測試完成!")

這樣看起來會更優雅一點,對之前寫的代碼改起來也方便一些

需要注意的是每個with塊只能有一個斷言,如果一個with下有多個斷言,當第一個斷言失敗的時候,后面的斷言就不會起作用的.

import pytest
from pytest import assume
# 以下這種是錯誤的示例,不要一個with下寫多個斷言
# 上海-悠悠


@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
        assert x+y > 1
        assert x > 1
    print("測試完成!")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM