![4ebef199bfb00f78d0b2ce875a9dec70.png [接口测试_B] 02 Pytest的简单示例_linux](https://file.cfanz.cn/uploads/jpeg/2023/03/31/14/7df8VeJ3RL.jpeg)
Pytest是什么
Pytest是Python的一个测试工具,可以用于所有类型和级别的软件测试。Pytest是一个可以自动查找到你编写的用例并运行后输出结果的测试框架。
Pytest有什么特点
- pytest是一个命令行工具
- pytest可以扩展第三方插件
- pytest易于持续集成和应用于web自动化测试
- pytest编写用例简单,并具有很强的可读性
- pytest可以直接采用assert进行断言,不必采用self.assertEqual()等
- pytest可以运行unittest编写的用例
- pytest可以运行以test或test开头或结尾的包、文件和方法
Pytest的简单示例
# test_simple.py
import requestsdef test_one():
r = requests.get('https://api.github.com/events')
assert r.status_code == 200运行测试用例可以直接在命令行中执行该py文件 pytest test_simple.py
![155545eca931823e7c39134fc412920e.png [接口测试_B] 02 Pytest的简单示例_单元测试_02](https://file.cfanz.cn/uploads/jpeg/2023/03/31/15/K121H117aU.jpeg)
运行py文件中的单个用例
# test_simple.py
import requests
def test_one():
r = requests.get('https://api.github.com/events')
assert r.status_code == 200
def test_two():
r = requests.get('https://api.github.com/events')
assert r.encoding == 'utf'运行py文件中的单个用例时,可以采用命令pytest test_simple.py::test_two
![702af81d96317da7f93f5f7f831943ad.png [接口测试_B] 02 Pytest的简单示例_java_03](https://file.cfanz.cn/uploads/jpeg/2023/03/31/15/I8Y3767039.jpeg)
由于断言失败,从结果中可以看到失败的具体原因。










