Python Testing

使用 pytest、TDD 方法论、测试夹具(Fixtures)、模拟(Mocking)、参数化(Parametrization)以及覆盖率要求的 Python 测试策略。

2,037 stars 332 forksTesting & QA Plugin marketplace

Install

Claude Code plugin
/plugin marketplace add xu-xiang/everything-claude-code-zh

The repo is a Claude Code plugin marketplace. Run /plugin afterwards to pick the plugin that includes this skill.

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

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 Testing Patterns)

使用 pytest、测试驱动开发(TDD)方法论和最佳实践的 Python 应用程序综合测试策略。

## 何时激活

- 编写新的 Python 代码时(遵循 TDD:红、绿、重构)
- 为 Python 项目设计测试套件时
- 审查 Python 测试覆盖率时
- 搭建测试基础设施时

## 核心测试哲学

### 测试驱动开发 (TDD)

始终遵循 TDD 循环:

1. **红(RED)**:为期望的行为编写一个失败的测试
2. **绿(GREEN)**:编写最少的代码使测试通过
3. **重构(REFACTOR)**:在保持测试通过的同时改进代码

```python
# 步骤 1:编写失败的测试 (红)
def test_add_numbers():
    result = add(2, 3)
    assert result == 5

# 步骤 2:编写最简实现 (绿)
def add(a, b):
    return a + b

# 步骤 3:如果需要则进行重构 (重构)
```

### 覆盖率要求

- **目标**:80% 以上的代码覆盖率
- **关键路径**:要求 100% 覆盖率
- 使用 `pytest --cov` 来衡量覆盖率

```bash
pytest --cov=mypackage --cov-report=term-missing --cov-report=html
```

## pytest 基础

### 基本测试结构

```python
import pytest

def test_addition():
    """测试基本的加法。"""
    assert 2 + 2 == 4

def test_string_uppercase():
    """测试字符串转大写。"""
    text = "hello"
    assert text.upper() == "HELLO"

def test_list_append():
    """测试列表追加。"""
    items = [1, 2, 3]
    items.append(4)
    assert 4 in items
    assert len(items) == 4
```

### 断言(Assertions)
Read the full skill on GitHub

Details

Skill name
python-testing
Path
skills/python-testing/SKILL.md
License
MIT
Repo updated
5 March 2026

FAQ

What does the Python Testing skill do?

使用 pytest、TDD 方法论、测试夹具(Fixtures)、模拟(Mocking)、参数化(Parametrization)以及覆盖率要求的 Python 测试策略。

How do I install the Python Testing skill?

Claude Code plugin: /plugin marketplace add xu-xiang/everything-claude-code-zh. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Python Testing popular?

It lives in xu-xiang/everything-claude-code-zh, which has 2,037 GitHub stars and 332 forks.

Related skills

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