pytest 测试框架的介绍及使用
简介:
Python 测试框架 用于编写和运行单元测试、集成测试、功能测试等。它比 Python 自带的 unittest
更加简洁、灵活,支持插件扩展,已经成为 Python 测试的首选工具之一。
准备:
pip install pytest
验证是否安装成功:
pytest --version
命名规范:
说明 | 内容 |
---|---|
⚠️ 类名建议 | 必须以 Test 开头(如 TestCases ) |
🧪 命名规则 | 测试函数/方法名必须以 test_ 开头 |
📁 文件名 | 推荐以 test_ 开头或 _test.py 结尾 |
项目结构(建议):
project/
├── config/ # 配置文件
├── utils/ # 工具类
├── data/ # 测试数据(yaml/json/excel)
├── testcases/ # 测试用例目录
│ ├── test_login.py
│ └── test_register.py
├── conftest.py # 全局 fixture
├── pytest.ini # pytest 配置文件
├── run.py # 启动脚本(可选)
├── reports/ # 测试报告输出目录
└── requirements.txt # 依赖文件
运行指令:
命令 | 说明 |
---|---|
pytest |
运行所有测试(默认简洁模式) |
pytest -v |
显示详细测试名称和结果 |
pytest -q 或 --quiet |
静默模式,输出更少 |
pytest -s |
允许打印输出(如 print() ) |
pytest -x |
遇到第一个失败就停止测试 |
pytest --maxfail=2 |
允许最多失败 2 次后停止 |
pytest --tb=short |
简洁显示失败的 traceback |
pytest -k "add" |
只运行包含 "add" 的测试函数 |
pytest -m "smoke" |
只运行带有 @pytest.mark.smoke 的测试 |
演示示例:
import pytest
def test_case1(): # 正常用例不会展示信息在控制台
pass
def test_case2(): # assert断言,失败则用例失败
assert 1 != 1
class TestCases:
def test_case3(self): # ZeroDivisionError异常,导致用例失败
1/0
@pytest.mark.skip(reason="忽略跳过")
def test_case4(self): # 基于@pytest.mark 跳过用例
raise Exception
运行结果详解读:
①、测试会话启动信息
platform win32 -- Python 3.13.3, pytest-8.3.5, pluggy-1.5.0
rootdir: D:\Project\self_study_python
configfile: pytest.ini
plugins: anyio-4.9.0
collected 4 items
- platform win32:在 Windows 平台上运行
- Python 3.13.3:使用的 Python 版本
- pytest-8.3.5:当前安装的 pytest 版本
- pluggy 版本: pytest 的插件系统库
- rootdir:pytest 的项目根目录
- configfile:使用了
pytest.ini
配置文件 - plugins:加载了插件,如
anyio
- collected 3 items:共收集到 4 个测试用例
②、测试执行过程
test\pytest_demo\test_my_first_pytest.py .FFs
- 每个字符代表一个测试用例的执行结果:
.
:测试通过(PASSED)F
:测试失败(FAILED)s
:测试跳过(SKIPPED)
③、失败详情
============================================ FAILURES ============================================
____________________________________________ test_case2 __________________________________________
def test_case2(): # assert断言,失败则用例失败
> assert 1 != 1
E assert 1 != 1
test\pytest_demo\test_my_first_pytest.py:12: AssertionError
______________________________________ TestCases.test_case3 ______________________________________
self = <test.pytest_demo.test_my_first_pytest.TestCases object at 0x00000146C1D4F9D0>
def test_case3(self): # ZeroDivisionError异常,导致用例失败
> 1/0
E ZeroDivisionError: division by zero
test\pytest_demo\test_my_first_pytest.py:17: ZeroDivisionError
失败用例1:
- 测试用例名称:
test_case2
- 错误类型:
AssertionError
(assert断言失败) - 错误位置:
test_my_first_pytest.py
第 12 行 - 错误原因:代码中判断
1 != 1
,导致用例失败
失败用例2:
- 测试用例名称:
TestCases.test_case3
- 错误类型:
ZeroDivisionError
(除以零错误) - 错误位置:
test_my_first_pytest.py
第 17 行 - 错误原因:代码中写了
1 / 0
,导致用例失败
④、简要总结信息
FAILED test/pytest_demo/test_my_first_pytest.py::test_case2 - assert 1 != 1
FAILED test/pytest_demo/test_my_first_pytest.py::TestCases::test_case3 - ZeroDivisionError: division by zero
⑤、总耗时与统计
=========================== 2 failed, 1 passed, 1 skipped in 0.23s ===========================
- 2 failed:2 个测试失败
- 1 passed:1 个测试通过
- 1 skipped:1 个测试被跳过
- in 0.23s:总共耗时 0.23 秒
三大核心机制:
fixture 装饰器
mark 标签
hook 钩子
总结:
fixture
是为测试用例准备资源的“工具箱”,hook
是控制测试流程的“指挥官”,而 mark
则是给测试用例贴标签的“分类员”。
概念 | 角色 | 功能描述 |
---|---|---|
fixture |
工具箱 | 为测试提供所需的外部资源(如登录、数据库连接、测试数据等) |
hook |
指挥官 | 控制整个测试生命周期(如测试开始、结束、报告生成等) |
mark |
分类员 | 给测试用例打标签,用于筛选、跳过、参数化、分组等操作 |
评论区