Use this skill when writing new features, fixing bugs, or refactoring code. Enforces test-driven development with 80%+ coverage including unit, integration, and E2E tests.
View on GitHubxu-xiang/everything-claude-code-zh
everything-claude-code
February 5, 2026
Select agents to install to:
npx add-skill https://github.com/xu-xiang/everything-claude-code-zh/blob/main/skills/tdd-workflow/SKILL.md -a claude-code --skill tdd-workflowInstallation paths:
.claude/skills/tdd-workflow/# 测试驱动开发 (TDD) 工作流
此技能(Skill)确保所有代码开发都遵循具有全面测试覆盖率的测试驱动开发(TDD)原则。
## 何时启用
- 编写新功能或新特性
- 修复 Bug 或问题
- 重构现有代码
- 添加 API 接口
- 创建新组件
## 核心原则
### 1. 测试先于代码 (Tests BEFORE Code)
始终先编写测试,然后编写代码使测试通过。
### 2. 覆盖率要求
- 至少 80% 的覆盖率(单元测试 + 集成测试 + 端到端测试)
- 覆盖所有边缘情况
- 测试所有错误场景
- 验证边界条件
### 3. 测试类型
#### 单元测试 (Unit Tests)
- 单个函数和实用程序
- 组件逻辑
- 纯函数
- 辅助函数和工具类
#### 集成测试 (Integration Tests)
- API 接口
- 数据库操作
- 服务间交互
- 外部 API 调用
#### 端到端测试 (E2E Tests - Playwright)
- 关键用户流程
- 完整的工作流
- 浏览器自动化
- UI 交互
## TDD 工作流步骤
### 第 1 步:编写用户旅程 (User Journeys)
```
作为 [角色],我想要 [动作],以便 [收益]
示例:
作为一个用户,我想要通过语义搜索市场,
以便即使没有精确的关键词也能找到相关的市场。
```
### 第 2 步:生成测试用例
为每个用户旅程创建全面的测试用例:
```typescript
describe('Semantic Search', () => {
it('returns relevant markets for query', async () => {
// 测试实现
})
it('handles empty query gracefully', async () => {
// 处理边缘情况
})
it('falls back to substring search when Redis unavailable', async () => {
// 测试回退行为
})
it('sorts results by similarity score', async () => {
// 测试排序逻辑
})
})
```
### 第 3 步:运行测试(预期失败)
```bash
npm test
# 测试应该失败 - 因为我们还没有实现功能
```
### 第 4 步:编写代码
编写最少量的代码使测试通过:
```typescript
// 由测试引导的实现
export async function searchMarkets(query: string) {
// 在此处实现
}
```
### 第 5 步:再次运行测试
```bash
npm test
# 测试现在应该通过
```
### 第 6 步:重构 (Refactor)
在保持测试通过的同时提高代码质量:
- 消除重复
- 改进命名
- 优化性能
- 增强可读性
### 第 7 步:验证覆盖率
```bash
npm run test:coverage
# 验证是否达到 80% 以上的覆盖率
```
## 测试模式
### 单元测试模式 (Jest/Vitest)
```typescript
import { render, screen, fireEvent } from '@testing-library/react'
import { Button } from './Button'
describe('Button Component', () => {
it('renders with correct text', () => {
render(<Button>Click me</Button>)
expect(screen.getByText('Click me')).toBeInTheDocument()
})
it('calls onClick when clicked', () => {
const handleClick = jest.fn()
render(<Button onClick={handleClick}>Click</Button>)
fireEvent.click(screen.getByRole('button'))
expect(handleClick).toHaveBeenCalledTimes(1)
})
it('is