agentleFS
YuAICodeYuAICode/ai-skills/skills/test-gen-zh/SKILL.md

test-gen-zh

Skill1 starsChanged 6 months agoMITView on GitHub

给指定函数或文件生成测试,配合 TDD 习惯。当用户说"帮我生成测试 / 给这个函数写测试 / test-gen-zh / 帮我补测试用例"时触发。bin 探测测试框架,Claude 读实现并按框架惯例生成覆盖正常/边界/错误路径的测试。

The file

Files
SKILL.md

test-gen-zh — 测试用例生成

给指定函数或文件自动生成测试,配合 TDD 工作流,覆盖正常路径、边界值、错误路径,而不是凑覆盖率数字。

何时触发

用户说以下任意一种:

  • "帮我生成测试 / 给这个函数写测试 / 补测试用例"
  • "test-gen-zh <文件或函数名>"
  • "这个函数没测试,帮我写一下"
  • "TDD — 先给我写 test"

用法

1. 探测测试框架

bash <skill>/bin/detect-framework.sh [项目目录]
# 输出:go test | jest | vitest | flutter test | pytest | cargo test | unknown
# 默认当前目录;不存在或识别不了均输出 unknown

2. 流程

  1. 探测框架:跑 bin/detect-framework.sh <项目目录> 确认测试框架。若输出 unknown,询问用户用哪个框架后继续。
  2. 读实现:读目标函数 / 文件,理解:
    • 函数签名(参数类型、返回值、可能抛出的错误)
    • 核心逻辑分支
    • 依赖(外部调用、IO、全局状态)
  3. 参照已有测试风格:若项目里有现成测试文件,先读一个做风格参照(命名、断言库、fixture 写法)。
  4. 生成测试:按该框架惯例写测试,三条路径都要覆盖:
    • 正常路径:典型入参 → 预期返回值
    • 边界值:空值、零值、最大/最小、类型边界
    • 错误路径:非法输入、外部依赖失败、超时、panic/exception
  5. 确认可跑:给出完整可执行的测试命令,若有 mock/stub 依赖一并给出。

输出格式

## <文件/函数名> 测试生成报告

**框架**:`<框架>` (由 detect-framework.sh 探测)
**测试文件**:`<建议路径>`

### 覆盖清单
- [ ] 正常路径:<说明>
- [ ] 边界值:<说明>
- [ ] 错误路径:<说明>

### 测试代码

\`\`\`<语言>
<完整可运行的测试代码>
\`\`\`

### 运行方式

\`\`\`bash
<运行命令>
\`\`\`

硬规则

  1. 测真实行为,不是凑覆盖率:每个 case 必须有明确的"验证什么",不写"assert True"这类无效断言。
  2. 不臆造被测代码的行为:拿不准函数在某边界返回什么,先读实现确认,再写断言。不要猜。
  3. 遵循项目已有测试风格:命名、fixture、mock 方式都跟项目已有测试保持一致;若无已有测试则按框架官方惯例。
  4. 依赖隔离:有 IO / 网络 / DB 调用的地方给出 mock/stub/fake 方案,不要生成需要真实环境才能跑的测试。
  5. 输出可直接复制执行:测试代码完整(含 import),运行命令可直接粘贴跑。
  6. 不写任何被测代码:只写测试;若被测函数不存在,说明后让用户先实现。

边界

  • 不依赖外部服务;bin/detect-framework.sh 纯 bash + grep,离线可用。
  • 不修改被测文件。
  • 仅生成测试代码,不执行跑测试(可在"运行方式"里给命令)。
  • 框架识别不了时(unknown)会明确告知并询问用户。

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.