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

test-generator

Skill1 starsChanged 4 months agoMITView on GitHub

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

21 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use git-doc-updater, git-precommit and 10 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.
  • u9401066Skill

    git-doc-updater

    Auto-check and update key documentation before Git commits to keep docs in sync with code. Triggers: docs, 文檔, 更新文檔, sync docs, release, 發布.

    Also in 3 projects that use this

  • u9401066Skill

    git-precommit

    Orchestrate pre-commit workflow including Memory Bank sync, README/CHANGELOG/ROADMAP updates. Triggers: GIT, gc, push, commit, 提交, 準備 commit, 要提交了, git commit, pre-commit, 推送.

    Also in 3 projects that use this

  • nilecuiSkill

    code-refactor

    Perform bulk code refactoring operations like renaming variables/functions across files, replacing patterns, and updating API calls. Use when users request renaming identifiers, replacing deprecated code patterns, updating method calls, or making consistent changes across multiple locations.

    Also in 3 projects that use this

  • AutumnsGroveSkill

    api-designer

    Design and document RESTful and GraphQL APIs with OpenAPI/Swagger specifications, authentication patterns, versioning strategies, and best practices. Use for: (1) Creating API specifications, (2) Designing REST endpoints, (3) GraphQL schema design, (4) API authentication and authorization, (5) API versioning strategies, (6) Documentation generation

    Also in 3 projects that use this

  • ArogyaReddySkill

    changelog-generator

    Automatically creates user-facing changelogs from git commits by analyzing commit history, categorizing changes, and transforming technical commits into clear, customer-friendly release notes. Turns hours of manual changelog writing into minutes of automated generation.

    Also in 4 projects that use this

  • +7 more from u9401066

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.