跳到正文
Skillpedia
测试48k · #5933

tdd-workflow

它能做什么

这个技能可以帮你以有条理、可靠的方式编写代码。它会引导你完成一个循环:先编写一个失败的测试,再编写让测试通过的最少代码,最后提升代码质量。

适用场景

  • ✓开始开发新功能时。
  • ✓修复 bug 时。
  • ✓处理复杂逻辑时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/tdd-workflow/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/tdd-workflow/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我应用 TDD 循环,创建一个计算两个数字之和的函数。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会遵循一个循环:首先,你描述代码应该实现什么功能,AI 编写一个失败的测试。然后,AI 编写最简单的代码让测试通过。最后,AI 审查并改进代码,同时确保测试不会失败。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

TDD Workflow

Write tests first, code second.


1. The TDD Cycle

🔴 RED → Write failing test
    ↓
🟢 GREEN → Write minimal code to pass
    ↓
🔵 REFACTOR → Improve code quality
    ↓
   Repeat...

2. The Three Laws of TDD

  1. Write production code only to make a failing test pass
  2. Write only enough test to demonstrate failure
  3. Write only enough code to make the test pass

3. RED Phase Principles

What to Write

Focus Example
Behavior "should add two numbers"
Edge cases "should handle empty input"
Error states "should throw for invalid data"

RED Phase Rules

  • Test must fail first
  • Test name describes expected behavior
  • One assertion per test (ideally)

4. GREEN Phase Principles

Minimum Code

Principle Meaning
YAGNI You Aren't Gonna Need It
Simplest thing Write the minimum to pass
No optimization Just make it work

GREEN Phase Rules

  • Don't write unneeded code
  • Don't optimize yet
  • Pass the test, nothing more

5. REFACTOR Phase Principles

What to Improve

Area Action
Duplication Extract common code
Naming Make intent clear
Structure Improve organization
Complexity Simplify logic

REFACTOR Rules

  • All tests must stay green
  • Small incremental changes
  • Commit after each refactor

6. AAA Pattern

Every test follows:

Step Purpose
Arrange Set up test data
Act Execute code under test
Assert Verify expected outcome

7. When to Use TDD

Scenario TDD Value
New feature High
Bug fix High (write test first)
Complex logic High
Exploratory Low (spike, then TDD)
UI layout Low

8. Test Prioritization

Priority Test Type
1 Happy path
2 Error cases
3 Edge cases
4 Performance

9. Anti-Patterns

❌ Don't ✅ Do
Skip the RED phase Watch test fail first
Write tests after Write tests before
Over-engineer initial Keep it simple
Multiple asserts One behavior per test
Test implementation Test behavior

10. AI-Augmented TDD

Multi-Agent Pattern

Agent Role
Agent A Write failing tests (RED)
Agent B Implement to pass (GREEN)
Agent C Optimize (REFACTOR)

Remember: The test is the specification. If you can't write a test, you don't understand the requirement.

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题

“RED”“GREEN”“REFACTOR”分别是什么意思?

“RED”表示测试失败,“GREEN”表示代码让测试通过,“REFACTOR”表示在保证一切正常运行的前提下改进代码。

为什么要先写测试再写代码?

先写测试能让你清楚地知道代码需要实现什么,也有助于避免写出不必要的代码。

什么是“YAGNI”?

YAGNI 是 “You Aren't Gonna Need It” 的缩写,意思是“你不会需要它”。这是一条原则,提醒你目前只编写真正必要的代码。

详情

作者的原始描述

Test-Driven Development workflow principles. RED-GREEN-REFACTOR cycle.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5933
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能