test-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Generator
测试生成器
Automatically create or update tests for files changed in the current branch,
iterating until statement coverage reaches the target threshold (default 80%).
自动为当前分支中变更的文件创建或更新测试,循环执行直到语句覆盖率达到目标阈值(默认80%)。
Workflow
工作流程
- Detect changed files — run
scripts/detect-changes.sh [base-branch] - Discover project conventions — read existing tests, test config, and setup files
- Write / update tests — for each uncovered file, create or extend its test file
- Run coverage check — run
scripts/check-coverage.sh <threshold> [files...] - Iterate — if any file is below threshold, read the coverage report, add missing tests, repeat from step 4 (max 3 iterations)
- Report — summarize final per-file coverage to the user
- 检测变更文件 — 运行
scripts/detect-changes.sh [base-branch] - 识别项目约定 — 读取现有测试、测试配置和初始化文件
- 编写/更新测试 — 针对每个未达标覆盖率的文件,创建或扩展其测试文件
- 运行覆盖率检查 — 运行
scripts/check-coverage.sh <threshold> [files...] - 循环迭代 — 如果任何文件未达到阈值,读取覆盖率报告,补充缺失的测试,从步骤4重复执行(最多3次迭代)
- 生成报告 — 向用户汇总最终的单文件覆盖率情况
Step 1 — Detect Changed Files
步骤1 — 检测变更文件
bash
bash <skill-path>/scripts/detect-changes.sh main- Pass the base branch as argument (defaults to upstream or ).
main - Output: one source file path per line.
- Filters out: test files, config, styles, assets, lock files, ,
__mocks__/.test/setup
If no files are output, inform the user there are no testable changes.
bash
bash <skill-path>/scripts/detect-changes.sh main- 传入基准分支作为参数(默认上游分支或)。
main - 输出:每行一个源文件路径。
- 过滤排除:测试文件、配置文件、样式文件、资源文件、锁文件、、
__mocks__/目录下的文件。test/setup
如果没有输出文件,告知用户没有可测试的变更内容。
Step 2 — Discover Project Conventions
步骤2 — 识别项目约定
Before writing any test, read these to match existing style:
- Test config — ,
vitest.config.*(test section),vite.config.*Cargo.toml - Test setup — files referenced by in config
setupFiles - Existing test for the file — ,
<name>.test.ts, or<name>.spec.ts__tests__/<name>.ts - Neighboring tests — 1-2 test files in the same directory for style reference
- Project rules — ,
AGENTS.md,CLAUDE.md,.eslintrc*eslint.config.*
Key things to extract:
- Test framework and assertion style (e.g. ,
expect())assert - Import conventions (vs globals)
import { describe } from 'vitest' - Mock patterns (manual mocks, )
vi.mock() - File naming: vs
*.test.ts*.spec.ts - Any JSDoc / lint requirements on test files
在编写任何测试前,读取以下内容以匹配现有代码风格:
- 测试配置 — 、
vitest.config.*(测试章节)、vite.config.*Cargo.toml - 测试初始化 — 配置文件中引用的文件
setupFiles - 文件对应的现有测试 — 、
<name>.test.ts或<name>.spec.ts__tests__/<name>.ts - 相邻测试文件 — 同一目录下1-2个测试文件,作为风格参考
- 项目规则 — 、
AGENTS.md、CLAUDE.md、.eslintrc*eslint.config.*
需要提取的关键信息:
- 测试框架和断言风格(如、
expect())assert - 导入约定(对比全局导入)
import { describe } from 'vitest' - 模拟模式(手动模拟、)
vi.mock() - 文件命名规则:对比
*.test.ts*.spec.ts - 测试文件的任何JSDoc/ESLint要求
Step 3 — Write / Update Tests
步骤3 — 编写/更新测试
For each changed file without sufficient coverage:
If no test file exists → create one following the project naming convention.
If a test file exists → add new test cases; do not rewrite existing passing tests.
针对每个覆盖率未达标的变更文件:
如果不存在测试文件 → 遵循项目命名约定创建一个。
如果已存在测试文件 → 添加新的测试用例;不要重写已通过的现有测试。
Test quality guidelines
测试质量准则
- Test behavior, not implementation details.
- Cover: happy path, edge cases, error paths, boundary values.
- Use descriptive /
describeblock names that explain the expected behavior.it - For React components: prefer queries (
@testing-library/react,getByRole) overgetByText.querySelector - For functions: test return values and side effects, not internal calls.
- Mock external dependencies (API, file system, DB) but not the unit under test.
- If the project uses , consider property tests for pure utility functions (name:
fast-check).*.property.test.ts
- 测试行为,而非实现细节。
- 覆盖:正常路径、边界情况、错误路径、边界值。
- 使用描述性的/
describe块名称,明确预期行为。it - 对于React组件:优先使用查询方法(
@testing-library/react、getByRole)而非getByText。querySelector - 对于函数:测试返回值和副作用,而非内部调用。
- 模拟外部依赖(API、文件系统、数据库),但不要模拟被测单元。
- 如果项目使用,可为纯工具函数考虑属性测试(命名:
fast-check)。*.property.test.ts
Step 4 — Run Coverage Check
步骤4 — 运行覆盖率检查
bash
bash <skill-path>/scripts/check-coverage.sh 80 src/services/foo.ts src/utils/bar.ts- First argument: threshold percentage.
- Remaining arguments: filter output to only these files.
- Output lines: or
PASS 92.3% src/services/foo.ts.FAIL 65.0% src/utils/bar.ts
If the script fails to find coverage output, check that the coverage provider is installed
(, , etc.) and install if missing.
@vitest/coverage-v8@vitest/coverage-istanbulbash
bash <skill-path>/scripts/check-coverage.sh 80 src/services/foo.ts src/utils/bar.ts- 第一个参数:阈值百分比。
- 剩余参数:过滤输出,仅显示指定文件的结果。
- 输出格式:或
PASS 92.3% src/services/foo.ts。FAIL 65.0% src/utils/bar.ts
如果脚本无法找到覆盖率输出,请检查是否已安装覆盖率提供器(、等),若缺失则进行安装。
@vitest/coverage-v8@vitest/coverage-istanbulStep 5 — Iterate
步骤5 — 循环迭代
For each file:
FAIL- Read the coverage JSON (for vitest) to identify uncovered statements.
coverage/coverage-final.json - Add targeted test cases for uncovered branches/statements.
- Re-run coverage check.
- Repeat up to 3 iterations total. If still below threshold after 3 iterations, report remaining gaps to the user with suggestions.
针对每个标记为的文件:
FAIL- 读取覆盖率JSON文件(Vitest为),识别未覆盖的语句。
coverage/coverage-final.json - 针对未覆盖的分支/语句添加针对性测试用例。
- 重新运行覆盖率检查。
- 最多重复3次迭代。如果3次迭代后仍未达到阈值,向用户报告剩余的覆盖率缺口并给出建议。
Step 6 — Report
步骤6 — 生成报告
Summarize results:
undefined汇总结果:
undefinedTest Coverage Report
测试覆盖率报告
| File | Coverage | Status |
|---|---|---|
| src/services/foo.ts | 92.3% | PASS |
| src/utils/bar.ts | 81.0% | PASS |
| src/components/Baz.tsx | 73.5% | FAIL |
Target: 80% | Passed: 2/3
For FAIL files, briefly explain what remains uncovered and suggest next steps.| 文件 | 覆盖率 | 状态 |
|---|---|---|
| src/services/foo.ts | 92.3% | PASS |
| src/utils/bar.ts | 81.0% | PASS |
| src/components/Baz.tsx | 73.5% | FAIL |
目标: 80% | 通过: 2/3
对于标记为FAIL的文件,简要说明仍未覆盖的内容并给出后续步骤建议。Scripts
脚本说明
- — list changed source files needing tests
scripts/detect-changes.sh [base-branch] - — run tests with coverage and report per-file results
scripts/check-coverage.sh <threshold> [files...]
- — 列出需要测试的变更源文件
scripts/detect-changes.sh [base-branch] - — 运行带覆盖率统计的测试,并报告单文件结果
scripts/check-coverage.sh <threshold> [files...]