agentleFS
liuxiao20051106-progliuxiao20051106-prog/claude-skills/test-generator/SKILL.md

test-generator

Skill1 starsChanged 3 months agoMITView on GitHub

根据函数/方法代码自动生成单元测试骨架。自动识别项目测试框架和已有测试风格,覆盖正常路径、边界条件、异常处理、依赖 mock。用户说"生成测试"、"写单测"、"加测试"、"test this"、"单元测试"时触发。

25 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use log-analyzer, security-reviewer and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • nek1987Skill

    log-analyzer

    Parse and analyze application logs to identify errors, patterns, and insights.

    Also in 3 projects that use this

  • JeffallanSkill

    security-reviewer

    Identifies security vulnerabilities, generates structured audit reports with severity ratings, and provides actionable remediation guidance. Use when conducting security audits, reviewing code for vulnerabilities, or analyzing infrastructure security. Invoke for SAST scans, penetration testing, DevSecOps practices, cloud security reviews, dependency audits, secrets scanning, or compliance checks. Produces vulnerability reports, prioritized recommendations, and compliance checklists.

    Also in 3 projects that use this

  • GoogleSkill

    code-review

    Adversarial systems, concurrency, memory-model, and architecture code review protocol. Use this skill whenever asked to review code, PRs, diffs, or branches in Filament.

    Also in 4 projects that use this

The file

Files
SKILL.md

test-generator · 单元测试生成器

根据选中代码或指定函数,生成符合项目风格的单元测试骨架。

流程

1. 识别上下文

  • 检测项目使用的测试框架:检查 package.json、Cargo.toml、go.mod、requirements*.txt、pyproject.toml 等
  • 定位已有测试文件,学习:
    • 文件命名约定(*.test.ts / *.spec.ts / test_*.py / *_test.go)
    • 测试结构(describe/it、test_ 前缀函数、#[test])
    • mock/stub 方式(jest.mock、sinon、unittest.mock、gomock)
    • 断言风格(expect().toBe()、assert.Equal()、self.assertEqual())

2. 分析目标函数

  • 参数类型、返回值类型、泛型约束
  • 分支路径:每个 if/else、switch/case、循环的典型场景
  • 外部依赖:网络请求、文件 I/O、数据库、第三方 SDK
  • 可抛出的异常/错误

3. 生成测试骨架

describe('functionName', () => {
  // 正常路径
  it('should return expected result for valid input', () => { /* 典型输入 → 预期输出 */ })

  // 边界条件
  it('should handle empty input gracefully', () => { /* 空数组/空字符串/null/undefined */ })
  it('should handle boundary values correctly', () => { /* 临界值:0, -1, MaxInt, 单元素 */ })

  // 异常处理
  it('should throw when required argument is missing', () => { /* 参数校验 */ })
  it('should return error when external call fails', () => { /* 模拟依赖失败 */ })

  // 依赖调用
  it('should call dependency with correct arguments', () => { /* 验证 mock 被正确调用 */ })
})

4. 原则

  • 匹配现有风格:函数名、结构、断言方式全对齐项目已有代码,新人不觉得是两拨人写的
  • 只写能跑的逻辑:不填 // TODO、expect(true).toBe(true) 这类占位符
  • 一个测试一个断言:每个 it/test 只验证一件事,失败时一眼知道原因
  • 命名即文档:it('should do X when Y') / test_<函数名>_<场景>_<预期>
  • 不重复已有:先检查是否已有相同逻辑的测试,避免生成重复用例
  • 不过度 mock:只 mock I/O 和外部边界,纯函数和内部工具不过度隔离
  • 覆盖关键路径:线上出过问题的路径必须覆盖;优先覆盖主力路径而非冷门分支

5. 输出格式

直接在目标测试文件中追加代码,或新建测试文件(如果该函数还没有对应测试文件)。生成后:

  • 标注每个测试覆盖的场景(一行注释)
  • 如果项目有测试覆盖率门槛,提示当前覆盖变化

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.