既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
class TestAssert():
# 对一个判断是否是闰年的方法进行测试
def test_exception_typeerror(self):
with pytest.raises(TypeError):
is_leap_year.is_leap_year(‘ss’)
def test_true(self):
assert is_leap_year.is_leap_year(400) == True
运行结果:
PS E:\python_interface_test\requests_practice> pytest -q .\test_assert.py… [100%]2 passed in 0.31 seconds
2、将异常信息存储到一个变量中,变量的类型则为异常类,包含异常的type、value和traceback等信息
import sys
sys.path.append(“.”)
import requests
import pytest
import is_leap_year
class TestAssert():
def test_exception_value(self):
with pytest.raises(ValueError) as excinfo:
is_leap_year.is_leap_year(0)
assert “从公元一年开始” in str(excinfo.value)
assert excinfo.type == ValueError
3、可以在用例中定义抛出的异常信息是否与预期的异常信息匹配,若不匹配则用例执行失败
import sys
sys.path.append(“.”)
import requests
import pytest
import is_leap_year
class TestAssert():
def test_exception_match(self):
with pytest.raises(ValueError, match=r’公元33元年是从公元一年开始’) as excinfo:
is_leap_year.is_leap_year(0)
assert excinfo.type == ValueError
运行结果:
![](https://img-blog.csdn.net/20180408204523129?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXNvbmd6aXll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
将match中的Pattern该为能够匹配的信息,则该用例能够执行成功。
4、使用标记函数检查异常
>
> pytest.mark.xfail(raises=xx)
>
>
>
import sys
sys.path.append(“.”)
import requests
import pytest
import is_leap_year
class TestAssert():
@pytest.mark.xfail(raises=ValueError)
def test_a(self):
is_leap_year.is_leap_year(-100)
输出结果:
![](https://img-blog.csdn.net/20180408204544560?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXNvbmd6aXll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
#### 总结
pytest的断言方式非常简洁明确。本节主要介绍了对异常信息的断言,包括4种情况:
* 直接断言,不添加assert语句
* 将异常信息存储在变量中,再读取异常信息进行断言判断
* 对异常的输出信息进行断言,异常类型、异常输出信息同时匹配成功,用例才能执行成功
* 采用标记函数进行异常断言
>
> 作者: 乐大爷
>
>
> 博客:https://www.jianshu.com/u/39cef8a56bf9
>
>
> 声明:本文转载,著作权归作者所有。
>
>
>
![img](https://img-blog.csdnimg.cn/img_convert/2cd7089290473ea6d6e664f9ab1582fa.png)
![img](https://img-blog.csdnimg.cn/img_convert/b2a531e370fbb76b68a3db9923fa8a25.png)
![img](https://img-blog.csdnimg.cn/img_convert/5340916aff821eedfaed09b09690478c.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**