pytest多重断言的实现
当我们写用例断言时,往往一个断言结果是不够的,所以需要加入多重断言,而多重断言,当断言中间出现断言结果False时,会中断后续的断言执行,会导致测试用例执行结果的准确性不高
使用pytest框架的插件pytest-assume, 实现用例执行时,其中一个断言失败后,执行后续的断言
安装: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多重断言的实现的文章就介绍到这了,更多相关pytest多重断言内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!