testing-mastery
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Mastery — Unified Testing Skill
测试精通 —— 统一测试技能
Write tests that document intent, catch regressions, and run fast. Choose the right strategy for the right situation.
编写能够记录意图、捕获回归问题且运行速度快的测试,针对不同场景选择合适的测试策略。
Decision Tree: Which Testing Strategy?
决策树:该选择哪种测试策略?
Is this a new feature?
├─ YES → Use TDD (see references/tdd-cycle.md)
│ Write failing test → Minimal code → Refactor
└─ NO
├─ Is this a bug fix?
│ └─ YES → Write regression test first, then fix
├─ Is this a critical user flow (login, checkout)?
│ └─ YES → E2E test (see references/e2e-playwright.md)
└─ Is this business logic / data transformation?
└─ YES → Unit + Integration tests (see references/unit-integration.md)Is this a new feature?
├─ YES → Use TDD (see references/tdd-cycle.md)
│ Write failing test → Minimal code → Refactor
└─ NO
├─ Is this a bug fix?
│ └─ YES → Write regression test first, then fix
├─ Is this a critical user flow (login, checkout)?
│ └─ YES → E2E test (see references/e2e-playwright.md)
└─ Is this business logic / data transformation?
└─ YES → Unit + Integration tests (see references/unit-integration.md)Testing Pyramid
测试金字塔
/\ E2E (Few, ~10%)
/ \ Critical user flows only
/----\
/ \ Integration (Some, ~20%)
/--------\ API, DB, service contracts
/ \
/------------\ Unit (Many, ~70%)
Functions, classes, utilities /\ E2E (Few, ~10%)
/ \ Critical user flows only
/----\
/ \ Integration (Some, ~20%)
/--------\ API, DB, service contracts
/ \
/------------\ Unit (Many, ~70%)
Functions, classes, utilitiesCore Principles
核心原则
| Principle | Rule |
|---|---|
| AAA | Arrange → Act → Assert |
| Fast | Unit < 100ms, Integration < 1s |
| Isolated | No test depends on another |
| Behavior | Test WHAT, not HOW |
| Minimal | One assertion per test (ideally) |
| 原则 | 规则 |
|---|---|
| AAA | Arrange → Act → Assert |
| 快速 | 单元测试 < 100ms,集成测试 < 1s |
| 隔离 | 测试之间不存在依赖关系 |
| 行为导向 | 测试「是什么」,而非「如何实现」 |
| 最小化 | 理想情况下每个测试仅包含一个断言 |
Quick Reference
快速参考
| I need to... | Use | Reference |
|---|---|---|
| Build feature test-first | TDD (RED-GREEN-REFACTOR) | tdd-cycle.md |
| Write unit/integration tests | Mocking, data strategies, patterns | unit-integration.md |
| Test critical user flows in browser | E2E with Playwright | e2e-playwright.md |
| 我需要... | 采用方案 | 参考文档 |
|---|---|---|
| 测试优先的方式开发功能 | TDD(红-绿-重构) | tdd-cycle.md |
| 编写单元/集成测试 | Mock、数据策略、测试模式 | unit-integration.md |
| 在浏览器中测试核心用户流程 | 基于Playwright的E2E测试 | e2e-playwright.md |
Anti-Patterns (Universal)
反模式(通用)
| ❌ Don't | ✅ Do |
|---|---|
| Test implementation details | Test observable behavior |
| Write tests after shipping | Write tests before/during |
| Duplicate test code | Use factories & fixtures |
| Complex test setup | Simplify or split |
| Ignore flaky tests | Fix root cause |
| Skip cleanup | Reset state in teardown |
| Multiple asserts per test | One behavior per test |
| ❌ 不要 | ✅ 推荐 |
|---|---|
| 测试实现细节 | 测试可观测的行为 |
| 上线后再补测试 | 在开发前/开发过程中编写测试 |
| 重复编写测试代码 | 使用工厂方法和fixture |
| 测试设置逻辑复杂 | 简化逻辑或者拆分测试 |
| 忽略不稳定的测试 | 修复问题根因 |
| 跳过清理步骤 | 在teardown阶段重置状态 |
| 单个测试包含多个断言 | 每个测试仅验证一个行为 |
🔧 Runtime Scripts
🔧 运行脚本
| Script | Purpose | Command |
|---|---|---|
| Unified test execution | |
| Browser E2E testing | |
| With screenshot | | |
| Accessibility check | |
Remember: The test is the specification. If you can't write a test for it, you don't understand the requirement.
| 脚本 | 用途 | 命令 |
|---|---|---|
| 统一测试执行 | |
| 浏览器E2E测试 | |
| 带截图功能 | | |
| 无障碍检查 | |
记住: 测试就是规范。如果你无法为某个需求编写测试,说明你还没有理解该需求。