pytest多重斷言的實現(xiàn)
當我們寫用例斷言時,往往一個斷言結果是不夠的,所以需要加入多重斷言,而多重斷言,當斷言中間出現(xiàn)斷言結果False時,會中斷后續(xù)的斷言執(zhí)行,會導致測試用例執(zhí)行結果的準確性不高
使用pytest框架的插件pytest-assume, 實現(xiàn)用例執(zhí)行時,其中一個斷言失敗后,執(zhí)行后續(xù)的斷言
安裝:pip install pytest-assume
以下為使用示例:
import pytest from pytest_assume.plugin import assume class TestTwo: def test001(self): with assume: assert True with assume: assert 1 == 2 def test002(self): assert 1 == 1 if __name__ == '__main__': pytest.main(['-v', 'test_002.py'])
我們很容易在以下信息中找出相應的日志信息:
在行有E標記的信息中,我們可以清晰看到提示 pytest_assume.plugin.FailedAssumption: 1 Failed Assumptions
collecting ... collected 2 items
test_002.py::TestTwo::test001 FAILED [ 50%]
test_002.py::TestTwo::test002 PASSED [100%]
================================== FAILURES ===================================
_______________________________ TestTwo.test001 _______________________________
self = <test_002.TestTwo object at 0x000001B24B284C40>
def test001(self):
with assume:
assert True
with assume:
> assert 1 == 2
E pytest_assume.plugin.FailedAssumption:
E 1 Failed Assumptions:
E
E test_002.py:10: AssumptionFailure
E >> assert 1 == 2
E AssertionError: assert 1 == 2
E +1
E -2
test_002.py:10: FailedAssumption
============================== warnings summary ===============================
D:\Python3.9.10\lib\site-packages\_pytest\config\__init__.py:1126
D:\Python3.9.10\lib\site-packages\_pytest\config\__init__.py:1126: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_assume
self._mark_plugins_for_rewrite(hook)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ===========================
FAILED test_002.py::TestTwo::test001 - pytest_assume.plugin.FailedAssumption:
=================== 1 failed, 1 passed, 1 warning in 0.19s ====================
進程已結束,退出代碼 0
到此這篇關于pytest多重斷言的實現(xiàn)的文章就介紹到這了,更多相關pytest多重斷言內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
將不規(guī)則的Python多維數(shù)組拉平到一維的方法實現(xiàn)
這篇文章主要介紹了將不規(guī)則的Python多維數(shù)組拉平到一維的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Python產(chǎn)生batch數(shù)據(jù)的操作
這篇文章主要介紹了Python產(chǎn)生batch數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python使用bcrypt?或?Passlib?對系統(tǒng)用戶密碼進行哈希和驗證處理操作
在Python?開發(fā)中,我們可以引入bcrypt?或?Passlib?對系統(tǒng)用戶密碼進行哈希和驗證處理,以及介紹使用其他類庫實現(xiàn)常規(guī)加解密處理操作,需要的朋友可以參考下2024-08-08