关于 testgen
testgen 是用于生成测试的技能,面向希望依据项目语言和既有约定编写测试的开发者。它会识别目标与测试框架,并按文件类型加载相应技能,支持多种语言及端到端测试。
主要功能
- 01分析目标并检查现有测试
- 02识别项目测试框架与约定
- 03按文件类型路由测试生成
- 04选择测试重点与覆盖深度
- 05支持多种语言和端到端测试
- 06视觉模式可使用 Chrome DevTools MCP
适用场景
- 01为指定函数或文件生成测试
- 02为项目目录补充测试覆盖
- 03按项目现有惯例编写测试
- 04使用视觉模式测试界面
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ 0xdarkmatter/ claude-mods --skill testgen安装后在 Claude Code 中输入/testgen确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
frameworks.mdvisual-testing.mdassets/.gitkeepreferences/.gitkeepscripts/.gitkeep详细介绍
TestGen 技能 - AI 测试生成
生成全面的测试,自动检测框架,优先加载技能后分派到通用处理流程,并匹配项目约定。
架构
testgen <target> [--type] [--focus] [--depth]
│
├─→ Step 1: Analyze Target
│ ├─ File exists? → Read and parse
│ ├─ Function specified? → Extract signature
│ ├─ Directory? → List source files
│ └─ Find existing tests (avoid duplicates)
│
├─→ Step 2: Detect Framework (parallel)
│ ├─ package.json → jest/vitest/mocha/cypress/playwright
│ ├─ pyproject.toml → pytest/unittest
│ ├─ go.mod → go test
│ ├─ Cargo.toml → cargo test
│ ├─ composer.json → phpunit/pest
│ └─ Check existing test patterns
│
├─→ Step 3: Load Project Standards
│ ├─ AGENTS.md, CLAUDE.md conventions
│ ├─ Existing test file structure
│ └─ Naming conventions (*.test.ts vs *.spec.ts)
│
├─→ Step 4: Route to Test Generator (general-purpose + skill preload)
│ ├─ .ts → general-purpose, preload typescript-ops
│ ├─ .tsx/.jsx → general-purpose, preload react-ops
│ ├─ .vue → general-purpose, preload vue-ops
│ ├─ .py → general-purpose, preload python-pytest-ops
│ ├─ .go → general-purpose, preload go-ops
│ ├─ .rs → general-purpose, preload rust-ops
│ ├─ .php → general-purpose, preload laravel-ops
│ ├─ E2E/Cypress → general-purpose, preload cypress-ops
│ ├─ Playwright → general-purpose, preload typescript-ops
│ ├─ Shell/bash → general-purpose, preload bash-ops
│ ├─ --visual → Chrome DevTools MCP
│ └─ Multi-file → parallel general-purpose dispatch
│
├─→ Step 5: Generate Tests
│ ├─ Create test file in correct location
│ ├─ Follow detected conventions
│ └─ Include: happy path, edge cases, error handling
│
└─→ Step 6: Integration
├─ Auto-create task (TaskCreate) for verification
└─ Suggest: run tests, /review, /save
执行步骤
第 1 步:分析目标
# Check if target exists
test -f "$TARGET" && echo "FILE" || test -d "$TARGET" && echo "DIRECTORY"
# For function-specific: extract signature
command -v ast-grep >/dev/null 2>&1 && ast-grep -p "function $FUNCTION_NAME" "$FILE"
# Fallback to ripgrep
rg "(?:function|const|def|public|private)\s+$FUNCTION_NAME" "$FILE" -A 10
检查是否存在现有测试:
fd -e test.ts -e spec.ts -e test.js -e spec.js | rg "$BASENAME"
fd "test_*.py" | rg "$BASENAME"
第 2 步:检测框架
JavaScript/TypeScript:
cat package.json 2>/dev/null | jq -r '.devDependencies | keys[]' | grep -E 'jest|vitest|mocha|cypress|playwright|@testing-library'
Python:
grep -E "pytest|unittest|nose" pyproject.toml setup.py requirements*.txt 2>/dev/null
Go:
test -f go.mod && echo "go test available"
Rust:
test -f Cargo.toml && echo "cargo test available"
PHP:
cat composer.json 2>/dev/null | jq -r '.["require-dev"] | keys[]' | grep -E 'phpunit|pest|codeception'
第 3 步:加载项目规范
# Claude Code conventions
cat AGENTS.md 2>/dev/null | head -50
cat CLAUDE.md 2>/dev/null | head -50
# Test config files
cat jest.config.* vitest.config.* pytest.ini pyproject.toml 2>/dev/null | head -30
测试位置约定:
# JavaScript
src/utils/helper.ts → src/utils/__tests__/helper.test.ts # __tests__ folder
→ src/utils/helper.test.ts # co-located
→ tests/utils/helper.test.ts # separate tests/
# Python
app/utils/helper.py → tests/test_helper.py # tests/ folder
→ tests/utils/test_helper.py # mirror structure
# Go
pkg/auth/token.go → pkg/auth/token_test.go # co-located (required)
# Rust
src/auth.rs → src/auth.rs (mod tests { ... }) # inline tests
→ tests/auth_test.rs # integration tests
第 4 步:分派给测试生成器
分派优先使用技能:通用的 general-purpose 子代理会在生成测试前预加载相关的 -ops 技能。
| 文件模式 | 分派目标 | 预加载 |
|---|---|---|
*.ts | general-purpose | skills/typescript-ops/SKILL.md |
*.tsx, *.jsx | general-purpose | skills/react-ops/SKILL.md + skills/typescript-ops/SKILL.md |
*.vue | general-purpose | skills/vue-ops/SKILL.md + skills/typescript-ops/SKILL.md |
*.py | general-purpose | skills/python-pytest-ops/SKILL.md |
*.go | general-purpose | skills/go-ops/SKILL.md |
*.rs | general-purpose | skills/rust-ops/SKILL.md |
*.php | general-purpose | skills/laravel-ops/SKILL.md |
*.cy.ts, cypress/* | general-purpose | skills/cypress-ops/SKILL.md + skills/typescript-ops/SKILL.md |
*.spec.ts(Playwright) | general-purpose | skills/playwright-ops/SKILL.md + skills/typescript-ops/SKILL.md |
playwright/*, e2e/* | general-purpose | skills/playwright-ops/SKILL.md + skills/typescript-ops/SKILL.md |
*.sh, *.bash | general-purpose | skills/bash-ops/SKILL.md |
| (--visual 标志) | Chrome DevTools MCP | skills/typescript-ops/SKILL.md |
通过 Agent 工具调用:
Agent tool with subagent_type: "general-purpose" (or surviving specialist from table)
model: "sonnet"
Prompt includes:
- Skill preloading (domain knowledge):
"First, read these files for testing context:
- Read: skills/security-ops/references/owasp-top10-a01-a05.md
- Read: skills/security-ops/references/owasp-top10-a06-a10.md
- Read: skills/testing-ops/SKILL.md
- Read: [Preload column for the matched file pattern]"
- Source file content
- Function signatures to test
- Detected framework and conventions
- Requested test type and focus
特定语言的预加载项(追加到上文的预加载部分):
| 语言 | 附加预加载项 | 原因 |
|---|---|---|
| Python | skills/python-pytest-ops/SKILL.md | Fixture、标记、参数化、异步测试 |
| Go | skills/go-ops/SKILL.md | 表驱动测试、基准测试、testify |
| Rust | skills/rust-ops/SKILL.md | 属性测试、criterion、proptest |
第 5 步:生成测试
根据 --focus 确定测试类别:
| 焦点 | 生成内容 |
|---|---|
happy | 正常输入、预期输出 |
edge | 边界值、空输入、null |
error | 无效输入、异常、错误处理 |
all | 以上全部(默认) |
深度级别:
| 深度 | 覆盖范围 |
|---|---|
quick | 仅正常流程,每个函数 1-2 个测试 |
normal | 正常流程 + 常见边缘情况(默认) |
thorough | 全面:所有路径、模拟、异步 |
第 6 步:集成
自动创建任务:
TaskCreate:
subject: "Run generated tests for src/auth.ts"
description: "Verify generated tests pass and review edge cases"
activeForm: "Running generated tests for auth.ts"
建议后续步骤:
Tests generated: src/auth.test.ts
Next steps:
1. Run tests: npm test src/auth.test.ts
2. Review and refine edge cases
3. Use /save to persist tasks across sessions
分派详情
TypeScript/JavaScript(预加载 typescript-ops)
- 正确导入类型
- 泛型处理
- Async/await 模式
- 模拟类型
React/JSX(预加载 react-ops)
- React Testing Library 模式
- 组件渲染测试
- Hook 测试(renderHook)
- 无障碍查询(getByRole)
Vue(预加载 vue-ops)
- Vue Test Utils 模式
- Composition API 测试
- Pinia store 模拟
Python(预加载 python-pytest-ops)
- pytest fixture
- 参数化测试
- Mock/patch 模式
- 异步测试处理
Go(预加载 go-ops)
- 表驱动测试(
[]struct模式) testing.T和子测试(t.Run)- Testify 断言(检测到时)
- 基准测试函数(
testing.B) - 并行测试(
t.Parallel())
Rust(预加载 rust-ops)
#[test]属性函数#[cfg(test)]模块组织- 使用
#[should_panic]进行错误测试 - 使用 proptest/quickcheck 进行属性测试
PHP/Laravel(预加载 laravel-ops)
- PHPUnit/Pest 模式
- 数据库事务
- Factory 用法
E2E/Cypress(预加载 cypress-ops)
- 页面对象模式
- 自定义命令
- 网络存根
Shell/bash(预加载 bash-ops)
- bats / shunit2 模式
- 对外部命令进行桩替换
- 断言退出代码和 stderr
Playwright(预加载 typescript-ops)
- 页面对象模型模式
- 定位器策略
- 视觉回归测试
CLI 工具集成
| 工具 | 用途 | 备用方案 |
|---|---|---|
jq | 解析 package.json | Read 工具 |
rg | 查找现有测试 | Grep 工具 |
ast-grep | 解析函数签名 | ripgrep 模式 |
fd | 查找测试文件 | Glob 工具 |
| Chrome DevTools MCP | 视觉测试(--visual) | Playwright/Cypress |
优雅降级:
command -v jq >/dev/null 2>&1 && cat package.json | jq '.devDependencies' || cat package.json
参考文件
框架专属代码示例,请参阅:
frameworks.md- 所有受支持语言的完整测试示例visual-testing.md---visual标志的 Chrome DevTools 集成
集成
| 命令 | 关联 |
|---|---|
/review | 提交前审查生成的测试 |
/explain | 测试前理解复杂代码 |
/save | 跟踪测试覆盖率目标 |
评论
支持 Markdown