首页 > 其他分享 >pytest学习和使用19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume)

pytest学习和使用19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume)

时间:2023-03-17 13:32:26浏览次数:36  
标签:断言 19 py assert assume pytest test 100

(19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume))

1 引入

  • pytest使用assert进行断言,如果有多个断言,第一个失败了,那么后续的断言将不会执行;
  • 那么如果第一个断言失败了,还想继续保持执行后边的断言,那如何做?
  • 我们可以使用pytest-assume来解决这个问题,即使用多重断言插件。

2 pytest-assume安装

pip3 install pytest-assume
C:\Users\Administrator>pip3 install pytest-assume
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pytest-assume
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/5d/ecd54af3d3dc3f69e9cf527449a51bd5ca39cd47aaeab9a74e55603a9ce1/pytest_assume-2.4.3-py3-none-any.whl (6.0 kB)
Requirement already satisfied: pytest>=2.7 in d:\python37\lib\site-packages (from pytest-assume) (6.2.4)
Requirement already satisfied: six in d:\python37\lib\site-packages (from pytest-assume) (1.15.0)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (2.1.1)
Requirement already satisfied: atomicwrites>=1.0 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.4.0)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.10.2)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.13.1)
Requirement already satisfied: py>=1.8.2 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.10.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (0.4.4)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (1.1.1)
Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (20.8)
Requirement already satisfied: attrs>=19.2.0 in d:\python37\lib\site-packages (from pytest>=2.7->pytest-assume) (20.3.0)
Requirement already satisfied: zipp>=0.5 in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=2.7->pytest-assume) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python37\lib\site-packages (from packaging->pytest>=2.7->pytest-assume) (2.4.7)
Installing collected packages: pytest-assume
Successfully installed pytest-assume-2.4.3

3 assert断言

  • 先看pytest自带的assert断言效果:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/16 
# 文件名称:test_assume.py
# 作用:pytest-assume插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

def test_case():
    a = 100
    b = 200
    assert a + b < 100
    assert a - b > 0
    assert a * b == 20000
    assert a / b == 200
    assert (b - a) / a == 1


if __name__ == '__main__':
    pytest.main(["-s", "test_assume.py"])

  • 运行结果如下,可以看到第一个断言失败后,后边的断言是无法进行的:
collecting ... collected 1 item

test_assume.py::test_case FAILED                                         [100%]
test_case\test_j\test_assume.py:8 (test_case)
def test_case():
        a = 100
        b = 200
>       assert a + b < 100
E       assert (100 + 200) < 100

test_assume.py:12: AssertionError

断言失败

断言失败


================================== FAILURES ===================================
__________________________________ test_case __________________________________

    def test_case():
        a = 100
        b = 200
>       assert a + b < 100
E       assert (100 + 200) < 100

test_assume.py:12: AssertionError
=========================== short test summary info ===========================
FAILED test_assume.py::test_case - assert (100 + 200) < 100
============================== 1 failed in 0.15s ==============================

4 pytest-assume断言

  • 代码如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/16 
# 文件名称:test_assume.py
# 作用:pytest-assume插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest


# def test_case():
#     a = 100
#     b = 200
#     assert a + b < 100
#     assert a - b > 0
#     assert a * b == 20000
#     assert a / b == 200
#     assert (b - a) / a == 1


def test_case01():
    a = 100
    b = 200
    pytest.assume(a + b < 100)
    pytest.assume(a - b > 0)
    pytest.assume(a * b == 20000)
    pytest.assume(a / b == 200)
    pytest.assume((b - a) / a == 1)
    print("执行到这了~~~~~~~~~~")


if __name__ == '__main__':
    pytest.main(["-s", "test_assume.py"])

  • 执行结果如下,可以看到前边有断言失败的,后边的断言是继续进行的:
test_assume.py::test_case01 FAILED                                       [100%]执行到这了~~~~~~~~~~

test_case\test_j\test_assume.py:21 (test_case01)
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None

    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               3 Failed Assumptions:
E               
E               test_assume.py:25: AssumptionFailure
E               >>	pytest.assume(a + b < 100)
E               AssertionError: assert False
E               
E               test_assume.py:26: AssumptionFailure
E               >>	pytest.assume(a - b > 0)
E               AssertionError: assert False
E               
E               test_assume.py:28: AssumptionFailure
E               >>	pytest.assume(a / b == 200)
E               AssertionError: assert False

D:\Python37\lib\site-packages\six.py:702: FailedAssumption

断言失败

断言失败


================================== FAILURES ===================================
_________________________________ test_case01 _________________________________

tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None

    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               3 Failed Assumptions:
E               
E               test_assume.py:25: AssumptionFailure
E               >>	pytest.assume(a + b < 100)
E               AssertionError: assert False
E               
E               test_assume.py:26: AssumptionFailure
E               >>	pytest.assume(a - b > 0)
E               AssertionError: assert False
E               
E               test_assume.py:28: AssumptionFailure
E               >>	pytest.assume(a / b == 200)
E               AssertionError: assert False

D:\Python37\lib\site-packages\six.py:702: FailedAssumption
---------------------------- Captured stdout call -----------------------------
执行到这了~~~~~~~~~~
=========================== short test summary info ===========================
FAILED test_assume.py::test_case01 - pytest_assume.plugin.FailedAssumption: 
============================== 1 failed in 0.17s ==============================

标签:断言,19,py,assert,assume,pytest,test,100
From: https://blog.51cto.com/NoamaNelson/6127121

相关文章

  • 力扣196(MySQL)-删除重复的电子邮箱(简单)
    题目:表: Person编写一个SQL删除语句来删除所有重复的电子邮件,只保留一个id最小的唯一电子邮件。以任意顺序返回结果表。(注意:仅需要写删除语句,将自动对剩余结......
  • 接口自动化---数据库断言封装python
    接口自动化---数据库断言封装python前言:在接口测试响应验证中,通常可以通过接口响应值来验证,还可以通过查询数据库信息辅助来验证。接口测试数据清理1、通过Delete接口删......
  • pytest简介
    一.pytest单元测试框架1.什么是单元测试框架单元测试是指在软件开发当中,针对软件的最小单位(函数,方法)进行正确性的检查测试。2.单元测试框架java:junit和estingpython:......
  • pytest笔记——fixture作用范围
    一、前言在使用pytest测试框架的时候,会经常使用到fixture,fixture相对灵活,能更好的实现一些用例场景的前置以及后置的操作,但在使用的过程中也经常遇到各种问题,例如我明明已......
  • pytest断言
    pytest断言1、使用==、!=、<、>、>=、<=2、使用in和notin来测试包含或不包含3、使用true或falseclassTestAssert:deftest_assert(self):......
  • 力扣中198 打家劫舍
    动态规划 //存在数组里还能行写这个两行代码捋不清publicintrob(int[]nums){intlen=nums.length;intres=0;inttemp;......
  • Wallys|DR4019S|IPQ4019|Support 11ABGN/802.11AC
     Ifyou'reinthemarketforahigh-performanceWi-Fisolutionforyourbusinessorhome,looknofurtherthantheQualcommIPQ4019.Thiscutting-edgesyste......
  • P8682 [蓝桥杯 2019 省 B] 等差数列
    P8682[蓝桥杯2019省B]等差数列[蓝桥杯2019省B]等差数列题目描述数学老师给小明出了一道等差数列求和的题目。但是粗心的小明忘记了一部分的数列,只记得其中N......
  • P5219 无聊的水题 I
    P5219无聊的水题I计有标号树,容易想到\(\text{Prufer}\)序列,那么对于度数的限制即使,每一个数的出现次数要小于等于\(m-1\),且一定要有等于的,容斥一下,用小于等于\(m-1......
  • 《Spark商业案例与性能调优实战100课》第19课:商业案例之NBA篮球运动员大数据分析核心
    《Spark商业案例与性能调优实战100课》第19课:商业案例之NBA篮球运动员大数据分析核心业务逻辑代码实战......