亲宝软件园·资讯

展开

pytest多重断言的实现

leslie0727 人气:0

当我们写用例断言时,往往一个断言结果是不够的,所以需要加入多重断言,而多重断言,当断言中间出现断言结果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

加载全部内容

相关教程
猜你喜欢
用户评论