pytest简易教程(25):pytest断言
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
断言是验证软件实际结果是否和预期结果一致,如果不一致,程序会中止执行并给出失败信息
assert断言
pytest使用的是python自带的assert关键字来进行断言
如果断言失败,assert后面的代码不会执行
语法:
1 2 3 | assert <表达式> assert <表达式>,<描述>,如果断言失败,描述作为AssertionError的内容展示 |
示例一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/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 |
结果:
示例二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/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( "异常" )<br> @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()作为上下文管理器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/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 |
结果:
预期警告断言
警告断言与异常断言比较类似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/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) |
结果:
__EOF__

本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容