agentleFS
handsongicehandsongice/vibe-rules/skills/test-driven-development/SKILL.md

test-driven-development

Skill1 starsChanged 3 months agoMITView on GitHub

写新功能或修 bug 时,先写失败测试再写实现代码

191 other projects use a version of this, 7 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use diagnosing-superpowers, api-and-interface-design 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.
  • obraSkill

    diagnosing-superpowers

    Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

    Also in 6 projects that use this

  • LLl0k0laDSkill

    api-and-interface-design

    Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

    Also in 32 projects that use this

  • MorningStar0709Skill

    workflow-runner

    Run an agency-orchestrator style YAML workflow inside the current Trae conversation by simulating or coordinating multiple roles. Use when the user provides a `.yaml` or `.yml` workflow, asks for multi-role collaboration, says things like “用多个角色一起评审这个 PRD”“多角色协作”“先生成工作流再执行”“让产品经理、架构师、QA 一起看一下”, or wants roles such as product manager, architect, reviewer, or QA to work together. Supports layered execution, role prompting, variable interpolation, document-backed inputs, and parallel steps only when the current environment has a clear subagent mapping. Do not use for ordinary implementation plans, simple code changes, or tasks already covered by `writing-plans`, `executing-plans`, `subagent-driven-development`, or single-purpose review/debug skills.

    Also in 5 projects that use this

  • LLl0k0laDSkill

    using-agent-skills

    Use when starting a session or when you need to discover which skill applies to the current task. This is the meta-skill that governs how all other skills are discovered and invoked.

    Also in 25 projects that use this

  • MorningStar0709Skill

    chinese-git-workflow

    Configure Git for domestic platforms and manage branching strategies suitable for Chinese teams. Use when the user asks to configure remotes, set up SSH, or manage Git workflows for Gitee, Coding, Jihu GitLab, or CNB, including requests like "配置国内 Git", "设置 Gitee 远程仓库", "推荐适合小团队的 Git 流程".

    Also in 4 projects that use this

  • vinvcnSkill

    constraint-driven-development

    把一个项目的质量线确立为书面契约,并阻止 agent 悄悄降低它。通过访谈确定用户在意哪些维度,当用户心里没有数字时提供合理的默认阈值,把一切记录进 CONSTRAINTS.md,并盯住 diff 中质量线被削弱的迹象——新增的 @ts-ignore 或 eslint-disable 抑制注释、被跳过或删除的测试、被剥掉的断言、未实现的 stub、被调低的阈值。当没有任何质量线被写下来时使用;当用户说 "set up constraints" 或 "define our standards" 时;当用户希望把他们在意的维度——无障碍性、Web 性能、覆盖率——设置成被强制执行的约束时;当 agent 为了变绿不断静默检查或跳过测试时;当你需要一个覆盖率或性能阈值却不知道该选什么数字时;或者当 agent 写下的代码超出任何人会读的体量时。

    Also in 4 projects that use this

  • +5 more from LLl0k0laD

The file

Files
SKILL.md

TDD(测试驱动开发)

触发条件

写新功能、加行为、修 bug——只要要动生产代码,就先加载本 skill。

铁律

没写过一个失败的测试之前,不写生产代码。

写完实现才想起来写测试?把实现删了重来。不要"留着当参考"、不要"边写测试边改"、不要"参考一下"。删掉就是删掉,从测试重新写。

例外(要先问用户):一次性原型、生成代码、配置文件。

想"这次先跳过 TDD"?停下来——这就是合理化借口。

Red → Green → Refactor

RED:写一个失败测试

一个最小测试,描述应该发生什么:

test('失败操作会重试 3 次', async () => {
  let attempts = 0;
  const operation = () => {
    attempts++;
    if (attempts < 3) throw new Error('fail');
    return 'success';
  };

  const result = await retryOperation(operation);

  expect(result).toBe('success');
  expect(attempts).toBe(3);
});

要求:

  • 一个测试只测一个行为。名字里有"and"就拆。
  • 名字描述行为,不叫 test1 / works。
  • 测真实代码行为,不要只测 mock 的行为。

确认 RED:看它真的失败(必做,不许跳)

npm test path/to/test.test.ts

确认:

  • 测试是失败(不是报错)
  • 失败信息是你预期的
  • 失败原因是"功能还没实现",不是 typo

测试直接过了? 那你在测已有行为,测试写错了。 测试报错? 先修到它正确地失败。

GREEN:写最小实现

写最简单的让测试通过的代码。不要加功能、不要顺手重构、不要"改进"。

async function retryOperation<T>(fn: () => Promise<T>): Promise<T> {
  for (let i = 0; i < 3; i++) {
    try {
      return await fn();
    } catch (e) {
      if (i === 2) throw e;
    }
  }
  throw new Error('unreachable');
}

不要写:可选参数、backoff 策略、onRetry 回调——YAGNI。

确认 GREEN:看它真的通过(必做)

npm test path/to/test.test.ts

确认:

  • 这个测试过了
  • 其他测试也都还过
  • 输出干净(没 warning、没 error)

挂了?改代码,不要改测试。其他测试挂了?立刻修。

REFACTOR:清理

绿了之后才清理:去重、改名字、抽 helper。保持绿,不加新行为。

重复

下一个行为,再来一轮 RED。

修 bug 的流程

Bug:空邮箱被接受。

RED:

test('拒绝空邮箱', async () => {
  const result = await submitForm({ email: '' });
  expect(result.error).toBe('Email required');
});

确认 RED:

FAIL: 预期 'Email required',实际 undefined

GREEN:

function submitForm(data: FormData) {
  if (!data.email?.trim()) {
    return { error: 'Email required' };
  }
  // ...
}

确认 GREEN:PASS。

红旗:你在合理化

借口真相
"太简单不用测"简单代码也会坏,测试 30 秒。
"我先写完再补测试"后补的测试直接通过——证明不了它测对了。
"精神到了就行不用形式"先写测试回答"应该做什么",后写测试回答"这代码做了什么"。后者被你已经写好的代码带偏。
"我手动测过了"手动测没有记录、不能回归、压力下漏 case。
"删了我写的代码太浪费"沉没成本。留着一堆你不能信任的代码才是浪费。
"留着当参考"你一定会忍不住对照着改,那就变成了后补测试。删掉就是删掉。
"TDD 拖慢我"TDD 是务实路径:bug 在 commit 前被抓住,重构不慌。"务实"的捷径是生产环境 debug,那才慢。
"现有代码没测试"你在改它,就给它补测试。

完成前清单

  • 每个新函数/方法都有测试
  • 看过每个测试在实现前真的失败
  • 每个测试失败原因正确(缺功能,不是 typo)
  • 写的是最小实现
  • 所有测试通过
  • 输出干净
  • 边界和错误路径有覆盖

有勾不上的?你跳了 TDD。重来。

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.