test-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test 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

工作流程

  1. Detect changed files — run
    scripts/detect-changes.sh [base-branch]
  2. Discover project conventions — read existing tests, test config, and setup files
  3. Write / update tests — for each uncovered file, create or extend its test file
  4. Run coverage check — run
    scripts/check-coverage.sh <threshold> [files...]
  5. Iterate — if any file is below threshold, read the coverage report, add missing tests, repeat from step 4 (max 3 iterations)
  6. Report — summarize final per-file coverage to the user
  1. 检测变更文件 — 运行
    scripts/detect-changes.sh [base-branch]
  2. 识别项目约定 — 读取现有测试、测试配置和初始化文件
  3. 编写/更新测试 — 针对每个未达标覆盖率的文件,创建或扩展其测试文件
  4. 运行覆盖率检查 — 运行
    scripts/check-coverage.sh <threshold> [files...]
  5. 循环迭代 — 如果任何文件未达到阈值,读取覆盖率报告,补充缺失的测试,从步骤4重复执行(最多3次迭代)
  6. 生成报告 — 向用户汇总最终的单文件覆盖率情况

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:
  1. Test config
    vitest.config.*
    ,
    vite.config.*
    (test section),
    Cargo.toml
  2. Test setup — files referenced by
    setupFiles
    in config
  3. Existing test for the file
    <name>.test.ts
    ,
    <name>.spec.ts
    , or
    __tests__/<name>.ts
  4. Neighboring tests — 1-2 test files in the same directory for style reference
  5. Project rules
    AGENTS.md
    ,
    CLAUDE.md
    ,
    .eslintrc*
    ,
    eslint.config.*
Key things to extract:
  • Test framework and assertion style (e.g.
    expect()
    ,
    assert
    )
  • Import conventions (
    import { describe } from 'vitest'
    vs globals)
  • Mock patterns (manual mocks,
    vi.mock()
    )
  • File naming:
    *.test.ts
    vs
    *.spec.ts
  • Any JSDoc / lint requirements on test files
在编写任何测试前,读取以下内容以匹配现有代码风格:
  1. 测试配置
    vitest.config.*
    vite.config.*
    (测试章节)、
    Cargo.toml
  2. 测试初始化 — 配置文件中
    setupFiles
    引用的文件
  3. 文件对应的现有测试
    <name>.test.ts
    <name>.spec.ts
    __tests__/<name>.ts
  4. 相邻测试文件 — 同一目录下1-2个测试文件,作为风格参考
  5. 项目规则
    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
    describe
    /
    it
    block names that explain the expected behavior.
  • For React components: prefer
    @testing-library/react
    queries (
    getByRole
    ,
    getByText
    ) over
    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
    fast-check
    , consider property tests for pure utility functions (name:
    *.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:
    PASS 92.3% src/services/foo.ts
    or
    FAIL 65.0% src/utils/bar.ts
    .
If the script fails to find coverage output, check that the coverage provider is installed (
@vitest/coverage-v8
,
@vitest/coverage-istanbul
, etc.) and install if missing.
bash
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-istanbul
等),若缺失则进行安装。

Step 5 — Iterate

步骤5 — 循环迭代

For each
FAIL
file:
  1. Read the coverage JSON (
    coverage/coverage-final.json
    for vitest) to identify uncovered statements.
  2. Add targeted test cases for uncovered branches/statements.
  3. Re-run coverage check.
  4. Repeat up to 3 iterations total. If still below threshold after 3 iterations, report remaining gaps to the user with suggestions.
针对每个标记为
FAIL
的文件:
  1. 读取覆盖率JSON文件(Vitest为
    coverage/coverage-final.json
    ),识别未覆盖的语句。
  2. 针对未覆盖的分支/语句添加针对性测试用例。
  3. 重新运行覆盖率检查。
  4. 最多重复3次迭代。如果3次迭代后仍未达到阈值,向用户报告剩余的覆盖率缺口并给出建议。

Step 6 — Report

步骤6 — 生成报告

Summarize results:
undefined
汇总结果:
undefined

Test Coverage Report

测试覆盖率报告

FileCoverageStatus
src/services/foo.ts92.3%PASS
src/utils/bar.ts81.0%PASS
src/components/Baz.tsx73.5%FAIL
Target: 80% | Passed: 2/3

For FAIL files, briefly explain what remains uncovered and suggest next steps.
文件覆盖率状态
src/services/foo.ts92.3%PASS
src/utils/bar.ts81.0%PASS
src/components/Baz.tsx73.5%FAIL
目标: 80% | 通过: 2/3

对于标记为FAIL的文件,简要说明仍未覆盖的内容并给出后续步骤建议。

Scripts

脚本说明

  • scripts/detect-changes.sh [base-branch]
    — list changed source files needing tests
  • scripts/check-coverage.sh <threshold> [files...]
    — run tests with coverage and report per-file results
  • scripts/detect-changes.sh [base-branch]
    — 列出需要测试的变更源文件
  • scripts/check-coverage.sh <threshold> [files...]
    — 运行带覆盖率统计的测试,并报告单文件结果