Pytest断言的具体使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Pytest断言的具体使用

cherish1112365   2023-03-20 我要评论

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)

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们