test-driven-development
它能做什么
这份指南会让你的智能体先写一个失败的测试,再编写修复缺陷或增加功能的代码。适合希望证明修改有效、且不会破坏其他功能的开发者。
适用场景
- ✓当你请智能体添加新逻辑或修改现有行为时。
- ✓当有人报告了缺陷,你希望在修复前先用测试复现它时。
- ✓当你想为某个边界情况增加处理,又不破坏现有行为时。
技能链接
https://github.com/cloudflare/computer/blob/main/.agents/skills/test-driven-development/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“有一个缺陷导致任务标题被保存为空。请先为它写一个会失败的测试,然后再修复。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能遵循一个循环:先写一个会失败的测试,再写能让它通过的最少代码,然后在测试仍然通过的情况下进行整理。对于缺陷,它会先用测试复现该缺陷。纯配置或文档修改不适用。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Test-Driven Development
Overview
Write a failing test before writing the code that makes it pass. For bug fixes, reproduce the bug with a test before attempting a fix. Tests are proof — "seems right" is not done. A codebase with good tests is an AI agent's superpower; a codebase without tests is a liability.
When to Use
- Implementing any new logic or behavior
- Fixing any bug (the Prove-It Pattern)
- Modifying existing functionality
- Adding edge case handling
- Any change that could break existing behavior
When NOT to use: Pure configuration changes, documentation updates, or static content changes that have no behavioral impact.
The TDD Cycle
RED GREEN REFACTOR
Write a test Write minimal code Clean up the
that fails ──→ to make it pass ──→ implementation ──→ (repeat)
│ │ │
▼ ▼ ▼
Test FAILS Test PASSES Tests still PASS
Step 1: RED — Write a Failing Test
Write the test first. It must fail. A test that passes immediately proves nothing.
// RED: This test fails because createTask doesn't exist yet
describe('TaskService', () => {
it('creates a task with title and default status', async () => {
const task = await taskService.createTask({ title: 'Buy groceries' });
expect(task.id).toBeDefined();
expect(task.title).toBe('Buy groceries');
expect(task.status).toBe('pending');
expect(task.createdAt).toBeInstanceOf(Date);
});
});
Step 2: GREEN — Make It Pass
Write the minimum code to make the test pass. Don't over-engineer:
// GREEN: Minimal implementation
export async function createTask(input: { title: string }): Promise<Task> {
const task = {
id: generateId(),
title: input.title,
status: 'pending' as const,
createdAt: new Date(),
};
await db.tasks.insert(task);
return task;
}
Step 3: REFACTOR — Clean Up
With tests green, improve the code without changing behavior:
- Extract shared logic
- Improve naming
- Remove duplication
- Optimize if necessary
Run tests after every refactor step to confirm nothing broke.
The Prove-It Pattern (Bug Fixes)
When a bug is reported, do not start by trying to fix it. Start by writing a test that reproduces it.
Bug report arrives
│
▼
Write a test that demonstrates the bug
│
▼
Test FAILS (confirming the bug exists)
│
▼
Implement the fix
│
▼
Test PASSES (proving the fix works)
│
▼
Run full test suite (no regressions)
Example:
// Bug: "Completing a task doesn't update the completedAt timestamp"
// Step 1: Write the reproduction test (it should FAIL)
it('sets completedAt when task is completed', async () => {
const task = await taskService.createTask({ title: 'Test' });
const completed = await taskService.completeTask(task.id);
expect(completed.status).toBe('completed');
expect(completed.completedAt).toBeInstanceOf(Date); // This fails → bug confirmed
});
// Step 2: Fix the bug
export async function completeTask(id: string): Promise<Task> {
return db.tasks.update(id, {
status: 'completed',
completedAt: new Date(), // This was missing
});
}
// Step 3: Test passes → bug fixed, regression guarded
The Test Pyramid
Invest testing effort according to the pyramid — most tests should be small and fast, with progressively fewer tests at higher levels:
╱╲
╱ ╲ E2E Tests (~5%)
╱ ╲ Full user flows, real browser
╱──────╲
╱ ╲ Integration Tests (~15%)
╱ ╲ Component interactions, API boundaries
╱────────────╲
╱ ╲ Unit Tests (~80%)
╱ ╲ Pure logic, isolated, milliseconds each
╱──────────────────╲
The Beyonce Rule: If you liked it, you should have put a test on it. Infrastructure changes, refactoring, and migrations are not responsible for catching your bugs — your tests are. If a change breaks your code and you didn't have a test for it, that's on you.
Test Sizes (Resource Model)
Beyond the pyramid levels, classify tests by what resources they consume:
| Size | Constraints | Speed | Example |
|---|---|---|---|
| Small | Single process, no I/O, no network, no database | Milliseconds | Pure function tests, data tra |
常见问题
这只适用于新功能吗?
不是。它也适用于缺陷修复以及对现有行为的修改。
为什么测试必须先失败?
一个马上通过的测试证明不了什么,因此技能会先确认它在修复前是失败的。
什么时候可以跳过它?
纯配置更改、文档更新,或没有行为变化的静态内容。
详情
作者的原始描述
Drives development with tests. Use when implementing any logic, fixing any bug, or changing any behavior. Use when you need to prove that code works, when a bug report arrives, or when you're about to modify existing functionality.
repo: cloudflare/computer
热度
- GitHub 星标
- 9.8k
- 排名
- #12208
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
cloudflare
9.8k在开始工作前,指引你的智能体查阅 Workers、Durable Objects、wrangler 及其他服务对应的 Cloudflare 平台指南。
prose
9.8k教你的智能体用简明易懂的英文撰写代码注释、提交信息、README 和文档,避免行话、缩写和官腔。
pull-requests
9.8k教你的智能体撰写清晰的拉取请求描述,讲清一项修改的来龙去脉、它解决的问题,以及如何进行验证。
gardener-tasks
9.8k讲解如何编写和检查 Gardener 任务文件,它通过 GitHub Actions 运行 AI 维护作业,并限制每个作业的修改范围。