测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----断言assert的使用方法
收藏本文
作者:redrose2100 类别: 日期:2022-09-01 15:18:27 阅读:811 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) # 一、Python中为False的数值断言均失败 在Python语言中表示False的数值即各个数据类型的空值,如整数的0,字符串的空字符串,None值,空列表、空字典、空集合、空元组,布尔类型的False等,这里需要注意的是一个空格的字符串的值不是False,编写测试脚本如下 ```bash def test_demo01(): assert 0 def test_demo02(): assert 0.0 def test_demo03(): assert "" def test_demo04(): assert () def test_demo05(): assert {} def test_demo06(): assert [] def test_demo07(): assert None def test_demo08(): assert False def test_demo09(): assert " " ``` 执行结果如下,可以看出有8个用例失败,有1个用例通过,通过的那个用例即对空格字符串的判断,即空格字符串的布尔值是True,而空字符串的布尔值则是False。 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src collected 9 items test_demo.py FFFFFFFF. [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_demo01 ______________________________________________________________________________ def test_demo01(): > assert 0 E assert 0 test_demo.py:2: AssertionError _____________________________________________________________________________ test_demo02 ______________________________________________________________________________ def test_demo02(): > assert 0.0 E assert 0.0 test_demo.py:5: AssertionError _____________________________________________________________________________ test_demo03 ______________________________________________________________________________ def test_demo03(): > assert "" E AssertionError: assert '' test_demo.py:8: AssertionError _____________________________________________________________________________ test_demo04 ______________________________________________________________________________ def test_demo04(): > assert () E assert () test_demo.py:11: AssertionError _____________________________________________________________________________ test_demo05 ______________________________________________________________________________ def test_demo05(): > assert {} E assert {} test_demo.py:14: AssertionError _____________________________________________________________________________ test_demo06 ______________________________________________________________________________ def test_demo06(): > assert [] E assert [] test_demo.py:17: AssertionError _____________________________________________________________________________ test_demo07 ______________________________________________________________________________ _____________________________________________________________________________ test_demo08 ______________________________________________________________________________ def test_demo08(): > assert False E assert False test_demo.py:23: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_demo01 - assert 0 FAILED test_demo.py::test_demo02 - assert 0.0 FAILED test_demo.py::test_demo03 - AssertionError: assert '' FAILED test_demo.py::test_demo04 - assert () FAILED test_demo.py::test_demo05 - assert {} FAILED test_demo.py::test_demo06 - assert [] FAILED test_demo.py::test_demo07 - assert None FAILED test_demo.py::test_demo08 - assert False ===================================================================== 8 failed, 1 passed in 0.16s ====================================================================== ``` # 二、Python逻辑表达式为False的断言均失败 在Python中常用的逻辑判断表达式,比如==、!=、<、>、<=、>=、is、is not,in,not in,is None,is not None等,断言时均失败,编写测试用例代码如下 ```bash def test_demo01(): assert 1==2 def test_demo02(): assert 3<2 def test_demo03(): assert 3 is str def test_demo04(): assert 3 in [1,2,4] def test_demo05(): assert 0 is None ``` 执行结果如下,即五个测试用例均失败。 ```bash pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src collected 5 items test_demo.py FFFFF [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_demo01 ______________________________________________________________________________ def test_demo01(): > assert 1==2 E assert 1 == 2 test_demo.py:2: AssertionError _____________________________________________________________________________ test_demo02 ______________________________________________________________________________ def test_demo02(): > assert 3<2 E assert 3 < 2 test_demo.py:5: AssertionError _____________________________________________________________________________ test_demo03 ______________________________________________________________________________ def test_demo03(): test_demo.py:11: AssertionError _____________________________________________________________________________ test_demo05 ______________________________________________________________________________ def test_demo05(): > assert 0 is None E assert 0 is None test_demo.py:14: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_demo01 - assert 1 == 2 FAILED test_demo.py::test_demo02 - assert 3 < 2 FAILED test_demo.py::test_demo03 - assert 3 is str FAILED test_demo.py::test_demo04 - assert 3 in [1, 2, 4] FAILED test_demo.py::test_demo05 - assert 0 is None ========================================================================== 5 failed in 0.14s =========================================================================== ``` 在Pytest中,断言assert语句的使用就是这么简单,主要看assert后的数值或者表达式的布尔值,如果为True,则用例通过,如果为False,则用例失败。 # 三、自定义断言报错信息 在自定义断言报错信息之前,首先观察一下默认的情况的报错信息,编写测试代码如下就 ```bash def test_demo(): a=5 assert a==10 ``` 执行结果如下,仔细观察也是能看出来错误原因的,只不过我们测试人员都是追求极致的简单,希望一样就能看出错误的原因。 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src collected 1 item test_demo.py F [100%] =============================================================================== FAILURES =============================================================================== ______________________________________________________________________________ test_demo _______________________________________________________________________________ def test_demo(): a=5 > assert a==10 E assert 5 == 10 test_demo.py:3: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_demo - assert 5 == 10 ========================================================================== 1 failed in 0.11s =========================================================================== ``` 编写如下代码 ```bash def test_demo(): a=5 assert a==10 , "变量a期望是10,当前为%d" % a ``` 执行结果如下,可以很快的从断言报错信息中看出断言失败的原因,即“变量a的值期望的是10,当前为5”,这样一来对自动化脚本调试和执行时定位问题等非常方便。 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src collected 1 item test_demo.py F [100%] =============================================================================== FAILURES =============================================================================== ______________________________________________________________________________ test_demo _______________________________________________________________________________ def test_demo(): a=5 > assert a==10 , "变量a期望是10,当前为%d" % a E AssertionError: 变量a期望是10,当前为5 E assert 5 == 10 test_demo.py:3: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_demo - AssertionError: 变量a期望是10,当前为5 ========================================================================== 1 failed in 0.11s =========================================================================== ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/360
上一篇:
Pytest----Pytest脚本的加载原理
下一篇:
Pytest----如何对捕获的异常的类型和内容进行断言
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件