让 Pytest 更美观:改进测试输出的完整指南(含插件与示例)
Source: Dev.to
Pytest 已经是 Python 中最好的测试框架之一——但它的 默认输出是纯文本的,在运行大型测试套件时有时很难阅读。
如果你曾经盯着一大堆纯文本的失败信息,或希望测试结果拥有更多颜色、结构或交互性……那么你来对地方了。
在本指南中,我们将探讨:
- 为什么 pytest 的输出会是现在这个样子
- 如何使用社区插件提升 pytest 输出
- 不同输出风格的并列示例
- 适用于初学者、进阶用户和高级配置的插件
- 命令行用法、安装说明和配置技巧
无论你是 Django、FastAPI、数据或后端开发者——本指南将显著提升你的测试体验。
1. 为什么 Pytest 默认输出很乏味
默认的 pytest 输出:
collected 12 items
test_example.py .......F....
================================ FAILURES ================================
_______________________________ test_function ____________________________
AssertionError: assert 2 == 5
问题
- 没有颜色变化
- 失败信息淹没在
.和F的海洋中 - 运行数十或数百个测试时难以阅读
- CI 日志检查起来非常痛苦
所以我们来改进它。
2. 让 Pytest 更美观(必备插件)
| Plugin | Purpose |
|---|---|
| pytest-sugar | 更好看的进度条,彩色输出 |
| pytest-rich | 基于 Rich 的控制台格式化 |
| pytest-clarity | 改进断言差异显示 |
| pytest-cov | 带颜色和百分比的覆盖率报告 |
| pytest-instafail | 立即显示失败,实时反馈 |
| pytest-md | 为 CI 生成 Markdown 输出 |
| pytest-tldr | 极简摘要模式 |
| rich | 添加颜色 + 格式化引擎 |
下面一步步演示。
3. Pytest Sugar — 更好看的测试进度
最适合: 初学者,立竿见影的改进
安装
pip install pytest-sugar
示例输出
──────────────────────────────────────────────────────────────────────────── pytest session starts ────────────────────────────────────────────────────────────────────────────
platform linux -- Python 3.10, pytest-9.0.2
rootdir: /home/project
❱ megaproject/tests/crm/test_example.py ✓ ✓ ✓ ✓
4 passed in 0.14s
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
新增:
- 进度条
- Unicode 符号
- 彩色的通过/失败标记
- 更简洁的失败信息
自动启用
pytest.ini
[pytest]
addopts = -s --disable-warnings
安装后 pytest-sugar 会自动加载。
4. Pytest Rich — Rich 控制台输出(华丽颜色 + 布局)
最适合: 追求极致可读性的开发者
安装
pip install pytest-rich
运行
pytest --rich
使用 Rich 前
FAILED tests/test_api.py::test_get_user - AssertionError: 404 != 200
使用 Rich 后
────────────────────────────── FAILED tests/test_api.py::test_get_user ──────────────────────────────
❌ AssertionError: Expected HTTP 200, got 404
Path: /api/v1/user/
Payload: {"id": 1}
────────────────────────────────────────────────────────────────────────────────────────────────────
Rich 带来:
- 彩色面板
- 更好的回溯信息
- 改进的差异显示
- 在 CI(如 GitHub Actions)中也有极佳表现
5. Pytest Clarity — 更好的断言差异
安装
pip install pytest-clarity
正常运行:
pytest
示例失败(默认)
E AssertionError: assert {'a': 1, 'b': 2} == {'a': 1, 'b': 3}
启用 Clarity 后
Comparing dicts:
b:
- 2
+ 3
可读性大幅提升!
6. Pytest Coverage — 高亮未覆盖代码
安装
pip install pytest-cov
使用覆盖率运行
pytest --cov=myapp --cov-report=term-missing
输出
----------- coverage: platform linux, python 3.10 -----------
Name Stmts Miss Cover Missing
----------------------------------------------------------
myapp/models.py 120 10 92% 144-152
myapp/views.py 80 25 69% 35-60
----------------------------------------------------------
TOTAL 200 35 83%
特性:
- 彩色编码的覆盖率
- 列出缺失的行号
- 汇总百分比
与 Django 配合使用非常顺畅。
7. Pytest Instafail — 立即显示失败
适用于测试套件较长的情况。
安装
pip install pytest-instafail
运行
pytest --instafail
现在失败会 在出现的瞬间 立即展示,而不是等所有测试结束后才显示。
8. 测试摘要插件
pytest‑tldr
提供极简的测试结果摘要。
安装
pip install pytest-tldr
添加到配置
pytest.ini
[pytest]
addopts = --tldr
输出
✨ 324 passed | 3 failed | 12 skipped
非常适合 CI 仪表盘。
9. 组合插件以获得最佳输出
推荐组合
pytest-sugar
pytest-rich
pytest-clarity
pytest-cov
pytest-instafail
pytest-tldr
写入 pytest.ini
[pytest]
addopts = --rich --tldr --cov=myapp --cov-report=term-missing
示例最终输出
🔥 Running tests with style…
───────────────────────── 12 passed, 1 failed, 2 skipped in 2.31s ─────────────────────────
----------- coverage: platform linux, python 3.10 -----------
Name Stmts Miss Cover Missing
-----------------------------------------------------------
myapp/models.py 120 10 92% 144-152
-----------------------------------------------------------
✨ Done! 92% coverage.
10. CI/CD 格式化的额外工具
pytest‑md(为 CI 生成 Markdown 输出)
pip install pytest-md
pytest --md=report.md
生成的 Markdown 可以附加到 CI 报告中。
pytest‑html
pip install pytest-html
pytest --html=report.html --self-contained-html
生成的自包含 HTML 报告适合与利益相关者共享。