---
name: Test Gen Expert
slug: test-gen-expert
category: Quality
description: Test Gen Expert helps write Python unit tests with AAA structure, clear naming, isolated fixtures, and correct mocking. Use it when generating or reviewing tests for normal, boundary, and error cases.
github: "https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/testgen"
language: Python
stars: 688
forks: 92
install: "npx degit https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/testgen ~/.claude/skills/testgen"
installs_to: ~/.claude/skills/testgen
source_path: kaiwu/builtin_experts/testgen/SKILL.md
collection_size: 19
category_size: 1354
collection_url: "https://dirskills.com/collections/val1813/kwcode"
added: 2026-08-12T04:43:51.053Z
last_synced: 2026-08-12T04:43:51.053Z
canonical_url: "https://dirskills.com/skills/test-gen-expert"
---

# Test Gen Expert

Test Gen Expert helps write Python unit tests with AAA structure, clear naming, isolated fixtures, and correct mocking. Use it when generating or reviewing tests for normal, boundary, and error cases.

**Install:**

```bash
npx degit https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/testgen ~/.claude/skills/testgen
```

## README

## 领域知识

你是单元测试专家。

### AAA 模式
每个测试严格分为 Arrange（准备数据）→ Act（执行操作）→ Assert（验证结果）三段，用空行分隔。

### 命名规范
test_<被测函数>_<场景>_<期望结果>，例如 test_login_wrong_password_returns_401。

### 隔离性
每个测试独立运行，不依赖其他测试的执行顺序或副作用。
共享状态用 fixture（scope='function'）重置。

### Mock 策略
外部依赖（网络请求/文件系统/数据库/时间）必须 mock。
mock 的 patch 路径指向被测模块里的名字，非原始库路径。
例：被测函数 from requests import get → patch('mymodule.get')。

### 边界覆盖
每个函数至少测试：正常输入、空输入、边界值、异常输入。
批量场景用 @pytest.mark.parametrize。

### 断言精确
一个测试一个核心断言。异常测试用 pytest.raises。浮点比较用 pytest.approx。

### 常见坑
1. mock 打错位置：必须 patch 被测模块里的名字
2. 不 mock 时间/随机数：用 freezegun 或 mock.patch 固定
3. 测试文件用相对路径读文件：改用 Path(__file__).parent 定位

## 经验规则（自动生成）
