Pytest 测试框架
Pytest 是什么?
- pytest 能够支持简单的单元测试和复杂的功能测试;
- pytest 可以结合 Requests 实现接口测试; 结合 Selenium、Appium 实现自动化功能测试;
- 使用 pytest 结合 Allure 集成到 Jenkins 中可以实现持续集成。
- pytest 支持 315 种以上的插件;
为什么要选择 Pytest
- 丰富的第三方插件
- 报告
- 多线程
- 顺序控制
- 简单灵活
- 兼容 unittest
- 定制化插件开发
Pytest 安装与准备
Pytest 环境安装
- 前提:本地已配置完成Python环境
- 第一种方式
pip install pytest
- 第二种方式 PyCharm 直接安装
运行第一个脚本
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
运行输入:
D:\Project\WebDemo>pytest test_demo.py
======================================== test session starts ========================================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\Project\WebDemo
collected 1 item
test_demo.py F [100%]
============================================= FAILURES ==============================================
____________________________________________ test_answer ____________________________________________
def test_answer():
> assert inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_demo.py:7: AssertionError
====================================== short test summary info ======================================
FAILED test_demo.py::test_answer - assert 4 == 5
========================================= 1 failed in 0.08s =========================================
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)