日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pytest多重斷言的實現(xiàn)_python

作者:leslie0727 ? 更新時間: 2023-05-07 編程語言

當我們寫用例斷言時,往往一個斷言結果是不夠的,所以需要加入多重斷言,而多重斷言,當斷言中間出現(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

原文鏈接:https://blog.csdn.net/weixin_43877527/article/details/128534270

欄目分類
最近更新