Python Testing Patterns

Use when writing or restructuring Python tests with pytest, fixtures, mocks, async code, database boundaries, or CI gates; choose the smallest test layer that proves behavior and preserve diagnostic evidence.

9 stars 3 forksTesting & QA

Install

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add sandbaseai/workbuddy-skill --skill python-testing-patterns
Manual install
git clone --depth 1 https://github.com/sandbaseai/workbuddy-skill.git /tmp/workbuddy-skill
mkdir -p ~/.claude/skills
cp -r /tmp/workbuddy-skill/skills/python-testing-patterns ~/.claude/skills/python-testing-patterns

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

# Python 测试模式

Python 测试的目标是证明行为、边界和失败处理,而不是堆积断言或追求一个漂亮的覆盖率百分比。先选择测试层,再用 pytest 的 fixture、参数化、mock、异步支持和标记建立可重复的证据。

## 范围与安全门禁

开始前记录 Python/pytest 版本、目标模块、需求/不变量、依赖服务、测试数据来源、允许的副作用、秘密处理和 CI 门禁。

- 默认使用合成数据、隔离数据库和最小权限测试账号;不读取或写入生产数据。
- 日志、失败输出、coverage、快照和 CI artifact 不得包含 token、密码、个人数据或完整连接字符串。
- 数据库、队列、邮件、付款和外部 API 使用 sandbox、fake 或可回滚 fixture;边界不明时返回 `BLOCKED`。
- 测试代码不自动修改实现、关闭门禁、批准发布或扩大访问权限。

## 选择测试层

| 层级 | 证明什么 | 典型工具/边界 |
| --- | --- | --- |
| Unit | 单个函数、值对象、领域规则和错误 | 纯输入、少 fixture、无真实网络/数据库 |
| Component | 模块内协作、应用用例和端口契约 | fake/内存适配器、受控 fixture |
| Integration | ORM、事务、消息、HTTP 和真实 schema 交互 | 隔离依赖、迁移和清理证据 |
| Functional/E2E | 完整用户或服务旅程 | 专用环境,少量高价值路径 |
| Performance | 延迟、吞吐、资源和退化 | 固定数据/环境,独立基线 |

一个单元测试不应伪装成集成测试;mock 通过不代表真实依赖契约、事务或性能通过。

## 基本结构

每个测试尽量遵循 Arrange-Act-Assert,并让测试名描述输入、场景和可观察结果:

```python
def test_create_user_with_duplicate_email_returns_conflict(user_repo):
    # Arrange
    user_repo.save(User(email="a@example.test"))

    # Act
    result = create_user(user_repo, email="a@example.test")

    # Assert
    assert result.error_code == "conflict"
    assert user_repo.count() == 1
```

避免 `test_1`、`test_function` 这类无法表达行为的名称,也避免只断言实现细节。优先断言返回值、状态变化、公开事件、错误类型/代码和用户可见结果。

## Fixture 与隔离

- fixture 只提供一个清晰资源或前置条件,作用域越短越容易推理;
- 默认 function scope,较宽作用域必须证明没有可变共享状态或顺序依赖;
- 每个测试创建自己的对象、目录、队列消息和数据库行,并在成功/失败后清理;
- 并行运行时使用 worker/租户/临时目录唯一命名空间;
- fixture 失败应清晰显示是环境前置条件失败,而非业务断言失败。
Read the full skill on GitHub

Details

Skill name
python-testing-patterns
Path
skills/python-testing-patterns/SKILL.md
License
MIT
Repo updated
8 September 2026

FAQ

What does the Python Testing Patterns skill do?

Use when writing or restructuring Python tests with pytest, fixtures, mocks, async code, database boundaries, or CI gates; choose the smallest test layer that proves behavior and preserve diagnostic evidence.

How do I install the Python Testing Patterns skill?

skills CLI (Claude Code, Cursor, Codex and more): npx skills add sandbaseai/workbuddy-skill --skill python-testing-patterns. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Python Testing Patterns popular?

It lives in sandbaseai/workbuddy-skill, which has 9 GitHub stars and 3 forks.

Related skills

Browse the most popular Claude skills, all testing & qa skills, or write your own with the skills generator.