# Test Driven Development

> 测试驱动开发，红-绿-重构循环

- **Type:** Skill
- **Install:** `agentstack add skill-5tldr-claude-skills-test-driven-development`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [5tldr](https://agentstack.voostack.com/s/5tldr)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [5tldr](https://github.com/5tldr)
- **Source:** https://github.com/5tldr/claude-skills/tree/main/skills/test-driven-development

## Install

```sh
agentstack add skill-5tldr-claude-skills-test-driven-development
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# 测试驱动开发 (TDD)

基于 [obra/superpowers](https://github.com/obra/superpowers) 的 TDD 方法论。

## 核心原则

> **先写测试，再写代码。红-绿-重构。**

## 触发条件

当用户需要实现新功能、修复 Bug、或重构代码时激活。

## TDD 循环

### 🔴 红：写失败的测试
```
1. 明确要实现的功能
2. 写一个测试用例
3. 运行测试，确认失败
4. 失败原因应该是功能未实现
```

### 🟢 绿：写最少的代码让测试通过
```
1. 只写让测试通过的代码
2. 不要过度设计
3. 不要写额外功能
4. 运行测试，确认通过
```

### 🔵 重构：改进代码质量
```
1. 在测试保护下重构
2. 消除重复代码
3. 改善命名和结构
4. 运行测试，确认仍然通过
```

## 工作流程

```
┌─────────────────────────────────────┐
│                                     │
│   ┌─────────┐                       │
│   │  🔴 红   │ ← 写失败的测试        │
│   └────┬────┘                       │
│        │                            │
│        ▼                            │
│   ┌─────────┐                       │
│   │  🟢 绿   │ ← 写最少代码通过测试   │
│   └────┬────┘                       │
│        │                            │
│        ▼                            │
│   ┌─────────┐                       │
│   │  🔵 重构 │ ← 改进代码质量        │
│   └────┬────┘                       │
│        │                            │
│        └────────────────────────────┘
│                  重复
└─────────────────────────────────────┘
```

## 输出格式

```markdown
# 🧪 TDD 实现记录

## 功能需求
[需求描述]

## 实现计划
1. [ ] [测试用例1]
2. [ ] [测试用例2]
3. [ ] [测试用例3]

---

## 迭代1

### 🔴 红：测试用例
```[语言]
// 测试代码
```

**运行结果**：❌ 失败
**失败原因**：[原因]

### 🟢 绿：实现代码
```[语言]
// 实现代码
```

**运行结果**：✅ 通过

### 🔵 重构
[重构说明，如果有的话]

---

## 迭代2
[同上格式]

---

## 最终代码

### 测试文件
```[语言]
// 完整测试代码
```

### 实现文件
```[语言]
// 完整实现代码
```

## 测试覆盖
- 语句覆盖：X%
- 分支覆盖：X%
- 函数覆盖：X%
```

## 测试类型

### 单元测试
```
- 测试单个函数/方法
- 隔离外部依赖
- 快速执行
- 高覆盖率
```

### 集成测试
```
- 测试模块间交互
- 包含真实依赖
- 验证接口契约
- 中等执行速度
```

### 端到端测试
```
- 测试完整流程
- 模拟用户行为
- 验证业务场景
- 执行较慢
```

## 测试金字塔

```
        /\
       /  \
      / E2E \      ← 少量
     /──────\
    /  集成   \    ← 适量
   /──────────\
  /    单元     \  ← 大量
 /──────────────\
```

## 最佳实践

### 测试命名
```
// 格式：should_[预期行为]_when_[条件]
test('should return empty array when input is empty')
test('should throw error when user not found')
```

### 测试结构 (AAA)
```
// Arrange - 准备
const input = [1, 2, 3]

// Act - 执行
const result = sum(input)

// Assert - 断言
expect(result).toBe(6)
```

### FIRST 原则
```
F - Fast（快速）
I - Independent（独立）
R - Repeatable（可重复）
S - Self-validating（自验证）
T - Timely（及时）
```

## 常见陷阱

### ❌ 避免
- 先写代码再补测试
- 测试依赖执行顺序
- 测试实现细节而非行为
- 测试覆盖率追求100%但质量低
- 忽略边界条件

### ✅ 建议
- 严格遵循红-绿-重构
- 每个测试独立运行
- 测试公开接口和行为
- 关注有意义的覆盖率
- 覆盖边界和异常情况

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [5tldr](https://github.com/5tldr)
- **Source:** [5tldr/claude-skills](https://github.com/5tldr/claude-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-5tldr-claude-skills-test-driven-development
- Seller: https://agentstack.voostack.com/s/5tldr
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
