使用pytest進行斷言判斷的時候,爲了用例的精準性,常常會多個方面進行斷言,好比以下:python
斷言1:斷言響應的http的狀態git
斷言2:斷言響應返回的code值github
斷言3:斷言響應返回的json中的data字段是否符合預期。json
若是使用原生python的assert,就會遇到一個斷言失敗則所有失敗的狀況。好比說,斷言1結果爲Failed,那麼斷言2和斷言3都不會被執行。session
咱們但願斷言2和斷言3繼續執行,這樣咱們能獲取更多的斷言結果來判斷出接口哪裏出了問題,可以更好地進行問題定位,這時候該本文主角出現了:pytest-assume插件。ide
一個能夠容許pytest測試用例中執行多個失敗的斷言的插件(即上面斷言1,斷言2,斷言3都失敗的狀況下,三個斷言都能被執行)。函數
項目:https://github.com/astraw38/pytest-assume測試
說明:.net
該插件源自pytest-expect,而且作了一部分小的修改:插件
1. 支持showlocals(即pytest命令行的'-l'參數, 顯示執行過程當中的局部變量)。
2. 能夠全局使用,無需指定fixtrue裝飾器。(即任意test_xxx函數中都能用)。
3. 對斷言輸出作了一些格式上的美化。
#根據你python版本,可選擇pip3或者pip sudo pip3(pip) install git+https://github.com/astraw38/pytest-assume.git #或者 sudo pip3(pip) install pytest-assume
1. 一個對比原生assert和pytest-assume的測試用例
#!/usr/bin/env python3 #!coding:utf-8 import pytest @pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)]) def test_simple_assume(x, y): assert x == y #若是這個斷言失敗,則後續都不會執行 assert True assert False @pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)]) def test_pytest_assume(x, y): pytest.assume(x == y) #即便這個斷言失敗,後續仍舊執行 pytest.assume(True) pytest.assume(False)
輸入:
===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.8.2, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /Users/xxx/Desktop/pytest
plugins: assume-2.4.2, ordering-0.6collected 6 items (這裏執行了六個用例)
test_demo.py FFF [100%]
========================================================================================== 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):
assert x == y
assert True
> assert False (前兩個斷言成功,第三個斷言失敗了)
E assert Falsetest_demo.py:9: 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):
> assert x == y (第一個斷言失敗了,後續斷言不會被執行)
E assert 1 == 0test_demo.py:7: 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):
> assert x == y (第一個斷言失敗了,後續斷言不會被執行)
E assert 0 == 1test_demo.py:7: AssertionError
___________________________________________________________________________________ test_pytest_assume[1-1] ___________________________________________________________________________________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 1 Failed Assumptions:
E
E test_demo.py:15: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False (前兩個斷言成功,第三個斷言失敗了)/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/six.py:702: FailedAssumption
---------------------------------------------------------------------------------- Captured stdout teardown -----------------------------------------------------------------------------------
F
___________________________________________________________________________________ test_pytest_assume[1-0] ___________________________________________________________________________________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.py:13: AssumptionFailure
E >> pytest.assume(x == y) (第一個斷言失敗,後續繼續執行)
E AssertionError: assert False
E
E test_demo.py:15: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/six.py:702: FailedAssumption
---------------------------------------------------------------------------------- Captured stdout teardown -----------------------------------------------------------------------------------
F
___________________________________________________________________________________ test_pytest_assume[0-1] ___________________________________________________________________________________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.py:13: AssumptionFailure
E >> pytest.assume(x == y) (第一個斷言失敗,後續繼續執行)
E AssertionError: assert False
E
E test_demo.py:15: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/six.py:702: FailedAssumption
---------------------------------------------------------------------------------- Captured stdout teardown -----------------------------------------------------------------------------------
F
=================================================================================== short test summary info ===================================================================================
FAILED test_demo.py::test_simple_assume[1-1] - assert False
FAILED test_demo.py::test_simple_assume[1-0] - assert 1 == 0
FAILED test_demo.py::test_simple_assume[0-1] - assert 0 == 1
FAILED test_demo.py::test_pytest_assume[1-1] - pytest_assume.plugin.FailedAssumption:
FAILED test_demo.py::test_pytest_assume[1-0] - pytest_assume.plugin.FailedAssumption:
FAILED test_demo.py::test_pytest_assume[0-1] - pytest_assume.plugin.FailedAssumption:
====================================================================================== 6 failed in 0.19s ======================================================================================
這裏咱們能夠看出兩者的區別了,執行差別以下:
斷言類型 | 1,1 | 1,0 | 0,1 | 結論 |
assert | 斷言3失敗 | 斷言1失敗,斷言2和斷言3不執行 | 斷言1失敗,斷言2和斷言3不執行 | assert遇到斷言失敗則停下 |
pytest.assume | 斷言3失敗 | 斷言1失敗,斷言2和斷言3繼續執行 | 斷言1失敗,斷言2和斷言3繼續執行 | pytest.assume不管斷言結果,所有執行 |
2. 經過上下文管理器with使用pytest-assume
#!/usr/bin/env python3#!coding:utf-8
import pytest from pytest import assume @pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)]) def test_simple_assume(x, y): #使用上下文管理器的好處是不用顯示去try和finally捕獲異常,建議使用這種寫法,簡潔有效。 with assume: assert x == y with assume: assert True with assume: assert False
主要注意的是,若是上下文管理器裏面包含多個斷言,則只有第一個會被執行,如
#!/usr/bin/env python3 #!coding:utf-8 import pytest from pytest import assume @pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)]) def test_simple_assume(x, y): #使用上下文管理器的好處是不用顯示去try和finally捕獲異常,建議使用這種寫法,簡潔有效。 with assume: #只有第一個斷言會被執行! assert x == y assert True assert False
以上就是pytest-assume插件的詳解,部份內容參考了github項目中的示例,有問題請留言。
博主:測試生財
座右銘:專一測試與自動化,致力提升研發效能;經過測試精進完成原始積累,經過讀書理財奔向財務自由。
csdn:https://blog.csdn.net/ccgshigao