Pytest断言的具体使用

作者:cherish1112365 时间:2023-12-15 06:30:04 

Pytest使用的断言是使用python内置的断言assert。Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。即pytest测试结果为False的断言为断言失败即测试用例执行失败,反之为断言成功即测试用例执行成功。

断言使用场景:

  • 为测试结果作断言

  • 为断言不通过的结果添加说明信息

  • 为预期异常作断言

  • 为失败断言作自定义说明信息

assert断言方法

assert关键字后面接表达式,常用的assert断言方法如下:

  • 判断xx为真:assert xx

  • 判断xx不为真:assert not xx

  • 判断b包含a:assert a in b

  • 判断b不包含a:assert a not in b

  • 判断a等于b:assert a == b

  • 判断a不等于b:assert a != b

示例:

# 为测试结果作断言
 
import pytest
from func import *
 
class TestFunc:
 def test_add_by_class(self):
  assert add(2,3) == 5
# 为断言不通过的结果添加说明信息
 
def test_login():
    # 使用python内置的断言
    # "1是不等于2的"是断言失败后,抛出异常输出的自定义提示信息
    assert 1 == 2, "1是不等于2的"
    
test_login()
 
# 运行结果:
AssertionError: 1是不等于2的

异常断言Excepiton

异常断言即为断言抛出的异常是预期的异常,执行的测试用例是成功的。

使用pytest.raises()作为上下文管理器。当抛出异常时,可获取到对应的异常实例,然后断言它抛出的异常是不是预期的异常。当代码抛出异常时,如果和raises指定的异常类相匹配,则断言不会失败。

官方文档:How to write and report assertions in tests — pytest documentation

 with pytest.raises()执行结束后会生成一个ExceptionInfo的实例对象,该对象包含type , value, traceback属性。

# 变量存储该异常的所有信息
with pytest.raises(TypeError) as 变量:
   # 获取变量的所有属性,即type、value、traceback
   print(变量.__dict__)

注意:断言type的时候,异常类型是不需要加引号的。断言value值的时候需转换str类型,value属性可作异常的说明信息。

示例如下:

import pytest

def test_zero_division_long():
   with pytest.raises(ZeroDivisionError) as excinfo:
       1 / 0

# 断言异常类型 type
   assert excinfo.type == ZeroDivisionError
   # 断言异常 value 值
   assert "division by zero" in str(excinfo.value)

with pytest.raise(ZeroDivisionError)用于对于故意测试异常代码的情况(已知异常),断言通过用例执行成功显示passed,不通过会显示failed。

示例如下:

# 为预期异常作断言完整示例,执行用例为True

# ./func.py
def add(a,b):
   if isinstance(a,int) and isinstance(b,int):
       return a+b
   else:
       raise TypeError('数据类型错误')

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

# 正常测试用例
def test_add_by_class(self):
   assert add(2,3) == 5

# 异常测试用例,期望结果为抛出TypeError异常
def test_add_by_func_aaa(self,*args, **kwargs):
   with pytest.raises(TypeError) as E:
       add('3',4)
   print(E.type)
   print(E.value)
   print(E.traceback)  

# ./run_test.py
import pytest

if __name__ == '__main__':
pytest.main(['-v'])
# 木有抛出预期异常的示例,执行用例为False

# ./func.py
def add(a,b):
# 指定异常,从此处直接抛出异常
   raise NameError("名称错误")
   if isinstance(a,int) and isinstance(b,int):
       return a+b
   else:
       raise TypeError('数据类型错误')

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:
   # 异常测试用例,期望结果为爆出TypeError异常
   def test_add_by_func_aaa(self,*args, **kwargs):
       with pytest.raises(TypeError):
           add('3',4)

# ./run_test.py
import pytest

if __name__ == '__main__':
pytest.main(['-v'])

可将match关键字参数传递给上下文管理器pytest.raises(),来测试正则表达式与异常的信息表达形式是否匹配。match方法相当于re.search功能。

注意:这种方法只能断言value,不能断言type。

示例如下:

import pytest

def test_zero_division_long():
   with pytest.raises(ZeroDivisionError, match=".*zero.*") as excinfo:
       1 / 0

# match方法相当于re.search功能,即match="zero"也是允许的
def test_zero_division_long():
   with pytest.raises(ZeroDivisionError, match="zero") as excinfo:
       1 / 0

pytest. raised()函数还有另一种形式,在这里传递一个函数,该函数将使用给定的*args和**kwargs执行,并断言引发了给定的异常。在没有异常或错误异常的情况下,报告器将为您提供有用的输出。

pytest.raises(ExpectedException, func, *args, **kwargs)

断言某个测试用例中可能出现多个不同的预期异常的解决办法:

在with pytest.raises()中传入异常类型的参数,从传入一个异常类型,改变为传入一个异常类型组成的元组。同样只是传入一个参数。

示例如下:

# ./func.py
def add(a,b):
   raise NameError('名字错了')
   if isinstance(a,int) and isinstance(b,int):
       return a+b
   else:
       raise TypeError('数据类型错误')

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:
   # 异常测试用例,期望结果为爆出TypeError异常
   def test_add_by_func_aaa(self,*args, **kwargs):
   # 将预期中多个错误信息组成一个元组
       with pytest.raises((TypeError,NameError),match=r'.*错.*$') as E:
           add('3',4)

# ./run_test.py
import pytest

if __name__ == '__main__':
pytest.main(['-v'])

检查断言装饰器

检查异常装饰器@pytest.mark.xfail():用于对于检查未修复的错误,即可能发生的异常。断言通过用例执行成功会显示xfailed,不通过显示failed。

作用:检查是否有异常,不确定是否有异常。

示例如下:

# 单个异常时断言装饰器使用

@pytest.mark.xfail(raises=ZeroDivisionError)
def test_f():
   1 / 0
# 多个异常时断言装饰器使用
@pytest.mark.xfail(raises=(TypeError, NameError))
def test_add_by_func_aaa2():
   add("3", 4)

来源:https://blog.csdn.net/cherish1112365/article/details/128028225

标签:Pytest,断言
0
投稿

猜你喜欢

  • python对 MySQL 数据库进行增删改查的脚本

    2024-01-18 07:04:24
  • Python利用tkinter和socket实现端口扫描

    2022-05-08 11:53:23
  • 使用Python实现管理系统附源码

    2023-04-04 04:22:34
  • 新年快乐! python实现绚烂的烟花绽放效果

    2022-01-15 13:18:00
  • Oracle与MySQL的区别及优缺点

    2024-01-25 12:47:52
  • python sklearn库实现简单逻辑回归的实例代码

    2022-06-19 18:43:29
  • python基础pandas的drop()用法示例详解

    2023-06-11 15:11:39
  • opencv python 图像轮廓/检测轮廓/绘制轮廓的方法

    2022-08-13 06:54:59
  • Python切片用法实例教程

    2023-09-28 15:26:17
  • asp如何在本地机器上创建缓存?

    2010-06-18 19:27:00
  • Python学习之列表常用方法总结

    2021-11-24 18:43:19
  • Python 实现数据库更新脚本的生成方法

    2024-01-15 22:35:43
  • mysql联合索引的使用规则

    2024-01-15 06:18:14
  • Python编程中的异常处理教程

    2022-10-16 04:26:23
  • 全面详解JS正则中匹配技巧及示例

    2024-03-24 15:07:39
  • MySQL创建新用户、增加账户的2种方法及使用实例

    2024-01-14 12:54:55
  • python重复值处理得方法

    2023-07-03 20:57:43
  • Anaconda出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url的解决过程

    2021-05-12 11:30:27
  • Python按钮的响应事件详解

    2023-12-24 15:35:08
  • MySQL中的字符串模式匹配

    2010-03-09 16:30:00
  • asp之家 网络编程 m.aspxhome.com