遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(25):pytest断言

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

 

断言是验证软件实际结果是否和预期结果一致,如果不一致,程序会中止执行并给出失败信息

assert断言

pytest使用的是python自带的assert关键字来进行断言

如果断言失败,assert后面的代码不会执行

语法:
assert <表达式>

assert <表达式>,<描述>,如果断言失败,描述作为AssertionError的内容展示
 

示例一:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/


def test_1_x():
    assert True
def test_2_x():
    assert 'qzcsbj'
def test_3_in():
    assert 'cs' in 'qzcsbj'
def test_4_not():
    assert not True

  

结果:

 

示例二:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/


def test_1_num():
    assert 1 == 1
def test_2_str():
    assert "1" == "1"
def test_3_dic():
    assert {"name": "ren"} == {"name": "qzcsbj"}, "---fail"
def test_4_list():
    assert [1, 2] == [1, 2], "---fail"
def test_5_tuple():
    assert (1, 2) == (1, 3)

  

结果:

 

断言装饰器

详见:xfail方法raises参数,https://www.cnblogs.com/uncleyong/p/17957992#_label7

raises:抛出某类型异常,和用例中raise的异常类型一样,结果就是FAILED,否则结果是XFAIL

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest


@pytest.mark.xfail
def test_d():
    print("---test_d")
    raise Exception("异常")

@pytest.mark.xfail(reason="异常了")
def test_c():
    print("---test_c")
    raise Exception("异常")
@pytest.mark.xfail(raises=RuntimeError) def test_b(): print("---test_b") raise RuntimeError("运行时异常") @pytest.mark.xfail(raises=RuntimeError) def test_a(): print("---test_a") raise Exception("异常")

  

结果:

 

预期异常断言

编写引发异常的断言,可以使用pytest.raises()作为上下文管理器

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
def test_a():
    # 捕获特定异常;采用pytest.raises上下文管理预期异常
    # 哪怕with下面的代码发生了ZeroDivisionError类型的异常,整个用例不会认为是异常用例,认为是正常的
    with pytest.raises(ZeroDivisionError):
        1 / 0

def test_b():
    #  可以捕获异常,获取细节(异常类型、异常信息),后面使用
    #  下面通过ex来访问异常信息
    with pytest.raises(ZeroDivisionError) as ex:
        1 / 0
    print("---ex:",ex.value)
    # 断言异常value值
    assert "division" in str(ex.value)
    # 断言异常类型
    assert ex.type == ZeroDivisionError

def test_c():
    # 用正则匹配异常信息
    with pytest.raises(ZeroDivisionError, match=".*division.*") as ex:
        1 / 0
    pass

 

结果:

 

预期警告断言

警告断言与异常断言比较类似

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
import warnings

# 下面写法,产生的警告不会打印出来
def test_warning_assert():
    with pytest.warns(UserWarning):
        warnings.warn("自定义警告1", UserWarning)

# 可以使用record获取警告信息
def test_warning_assert2():
    with pytest.warns(RuntimeWarning) as record:
        warnings.warn("自定义警告2", RuntimeWarning)
    assert len(record) == 1
    assert record[0].message.args[0] == "自定义警告2"

# match通过正则匹配异常信息中的关键字
def test_warning_assert3():
    with pytest.warns(UserWarning, match=".*自定义.*3"):
        warnings.warn("自定义警告3", UserWarning)

 

结果:

 

 

 

posted @ 2024-02-23 22:22  全栈测试笔记  阅读(138)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end