testing-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Patterns
测试模式
Principles for reliable test suites.
构建可靠测试套件的原则。
1. Testing Pyramid
1. 测试金字塔
/\ E2E (Few)
/ \ Critical flows
/----\
/ \ Integration (Some)
/--------\ API, DB queries
/ \
/------------\ Unit (Many)
Functions, classes /\ E2E(少量)
/ \ 关键流程
/----\
/ \ 集成测试(适量)
/--------\ API、数据库查询
/ \
/------------\ 单元测试(大量)
函数、类2. AAA Pattern
2. AAA 模式
| Step | Purpose |
|---|---|
| Arrange | Set up test data |
| Act | Execute code under test |
| Assert | Verify outcome |
| 步骤 | 目的 |
|---|---|
| Arrange | 设置测试数据 |
| Act | 执行待测试代码 |
| Assert | 验证结果 |
3. Test Type Selection
3. 测试类型选择
When to Use Each
适用场景
| Type | Best For | Speed |
|---|---|---|
| Unit | Pure functions, logic | Fast (<50ms) |
| Integration | API, DB, services | Medium |
| E2E | Critical user flows | Slow |
| 类型 | 最佳适用场景 | 速度 |
|---|---|---|
| Unit(单元测试) | 纯函数、业务逻辑 | 快(<50ms) |
| Integration(集成测试) | API、数据库、服务 | 中等 |
| E2E | 关键用户流程 | 慢 |
4. Unit Test Principles
4. 单元测试原则
Good Unit Tests
优质单元测试的特征
| Principle | Meaning |
|---|---|
| Fast | < 100ms each |
| Isolated | No external deps |
| Repeatable | Same result always |
| Self-checking | No manual verification |
| Timely | Written with code |
| 原则 | 含义 |
|---|---|
| 快速 | 每个测试<100ms |
| 隔离 | 无外部依赖 |
| 可重复 | 结果始终一致 |
| 自校验 | 无需人工验证 |
| 及时 | 与代码同步编写 |
What to Unit Test
单元测试的测试范围
| Test | Don't Test |
|---|---|
| Business logic | Framework code |
| Edge cases | Third-party libs |
| Error handling | Simple getters |
| 需测试 | 无需测试 |
|---|---|
| 业务逻辑 | 框架代码 |
| 边缘情况 | 第三方库 |
| 错误处理 | 简单的 getter 方法 |
5. Integration Test Principles
5. 集成测试原则
What to Test
测试范围
| Area | Focus |
|---|---|
| API endpoints | Request/response |
| Database | Queries, transactions |
| External services | Contracts |
| 领域 | 重点 |
|---|---|
| API 端点 | 请求/响应 |
| 数据库 | 查询、事务 |
| 外部服务 | 契约 |
Setup/Teardown
前置/后置处理
| Phase | Action |
|---|---|
| Before All | Connect resources |
| Before Each | Reset state |
| After Each | Clean up |
| After All | Disconnect |
| 阶段 | 操作 |
|---|---|
| Before All | 连接资源 |
| Before Each | 重置状态 |
| After Each | 清理资源 |
| After All | 断开连接 |
6. Mocking Principles
6. 模拟(Mocking)原则
When to Mock
何时使用模拟
| Mock | Don't Mock |
|---|---|
| External APIs | The code under test |
| Database (unit) | Simple dependencies |
| Time/random | Pure functions |
| Network | In-memory stores |
| 需模拟 | 无需模拟 |
|---|---|
| 外部 API | 待测试代码 |
| 数据库(单元测试中) | 简单依赖 |
| 时间/随机函数 | 纯函数 |
| 网络请求 | 内存存储 |
Mock Types
模拟类型
| Type | Use |
|---|---|
| Stub | Return fixed values |
| Spy | Track calls |
| Mock | Set expectations |
| Fake | Simplified implementation |
| 类型 | 用途 |
|---|---|
| Stub | 返回固定值 |
| Spy | 跟踪调用情况 |
| Mock | 设置预期结果 |
| Fake | 简化实现 |
7. Test Organization
7. 测试组织
Naming
命名规范
| Pattern | Example |
|---|---|
| Should behavior | "should return error when..." |
| When condition | "when user not found..." |
| Given-when-then | "given X, when Y, then Z" |
| 模式 | 示例 |
|---|---|
| 行为描述 | "should return error when..."(当...时应返回错误) |
| 条件描述 | "when user not found..."(当用户未找到时...) |
| Given-when-then | "given X, when Y, then Z"(给定X,当执行Y时,结果为Z) |
Grouping
分组方式
| Level | Use |
|---|---|
| describe | Group related tests |
| it/test | Individual case |
| beforeEach | Common setup |
| 层级 | 用途 |
|---|---|
| describe | 分组相关测试 |
| it/test | 单个测试用例 |
| beforeEach | 公共前置设置 |
8. Test Data
8. 测试数据
Strategies
策略
| Approach | Use |
|---|---|
| Factories | Generate test data |
| Fixtures | Predefined datasets |
| Builders | Fluent object creation |
| 方法 | 用途 |
|---|---|
| Factories | 生成测试数据 |
| Fixtures | 预定义数据集 |
| Builders | 流畅式对象创建 |
Principles
原则
- Use realistic data
- Randomize non-essential values (faker)
- Share common fixtures
- Keep data minimal
- 使用真实感数据
- 非关键值随机化(使用faker)
- 共享通用测试数据
- 保持数据精简
9. Best Practices
9. 最佳实践
| Practice | Why |
|---|---|
| One assert per test | Clear failure reason |
| Independent tests | No order dependency |
| Fast tests | Run frequently |
| Descriptive names | Self-documenting |
| Clean up | Avoid side effects |
| 实践 | 原因 |
|---|---|
| 每个测试一个断言 | 明确失败原因 |
| 测试独立无依赖 | 无执行顺序依赖 |
| 测试执行快速 | 可频繁运行 |
| 命名具有描述性 | 自文档化 |
| 清理资源 | 避免副作用 |
10. Anti-Patterns
10. 反模式
| ❌ Don't | ✅ Do |
|---|---|
| Test implementation | Test behavior |
| Duplicate test code | Use factories |
| Complex test setup | Simplify or split |
| Ignore flaky tests | Fix root cause |
| Skip cleanup | Reset state |
Remember: Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.
| ❌ 不要做 | ✅ 应该做 |
|---|---|
| 测试实现细节 | 测试行为 |
| 重复测试代码 | 使用工厂方法 |
| 复杂的测试前置设置 | 简化或拆分 |
| 忽略不稳定测试 | 修复根本原因 |
| 跳过资源清理 | 重置状态 |
注意: 测试即文档。如果有人无法通过测试理解代码功能,请重写测试。