pytest多重断言的实现

作者:leslie0727 时间:2021-10-12 03:30:25 

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

来源:https://blog.csdn.net/weixin_43877527/article/details/128534270

标签:pytest,多重断言
0
投稿

猜你喜欢

  • 解决Python中导入自己写的类,被划红线,但不影响执行的问题

    2021-07-11 19:10:29
  • Python更换pip源方法过程解析

    2022-01-20 08:00:58
  • 使用PyTorch实现随机搜索策略

    2021-05-16 02:33:02
  • python获取代码运行时间的实例代码

    2023-11-04 02:25:10
  • oracle数据库冷备份的方法

    2023-07-19 09:51:19
  • PHP 检查扩展库或函数是否可用的代码

    2023-07-22 23:34:34
  • python常见字符串处理函数与用法汇总

    2023-10-19 08:07:11
  • Microsoft SQL Server 2000安装问题集锦

    2008-12-10 14:26:00
  • python去除删除数据中\\u0000\\u0001等unicode字符串的代码

    2023-09-04 09:15:51
  • 解决Python安装后pip不能用的问题

    2023-05-10 04:52:14
  • PHP session会话的安全性分析

    2023-11-21 23:47:59
  • 归纳万恶IE6的HACK方法

    2010-02-04 17:17:00
  • python 函数定位参数+关键字参数+inspect模块

    2023-07-05 23:04:37
  • 讲解MaxDB数据库和MySQL数据库的主要差别

    2009-01-04 13:04:00
  • python之excel文件(.xls文件)处理方式

    2021-08-22 15:58:14
  • python 容器总结整理

    2021-03-20 16:52:08
  • 网址站的2.0玩法

    2010-03-15 12:25:00
  • 使用Alt提升可访问性

    2009-04-04 19:22:00
  • oracle跨库查询的方法

    2023-07-18 03:15:35
  • 谈谈FACEBOOK的一处产品细节

    2008-03-11 11:05:00
  • asp之家 网络编程 m.aspxhome.com