testing-mastery

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing 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, utilities

Core Principles

核心原则

PrincipleRule
AAAArrange → Act → Assert
FastUnit < 100ms, Integration < 1s
IsolatedNo test depends on another
BehaviorTest WHAT, not HOW
MinimalOne assertion per test (ideally)

原则规则
AAAArrange → Act → Assert
快速单元测试 < 100ms,集成测试 < 1s
隔离测试之间不存在依赖关系
行为导向测试「是什么」,而非「如何实现」
最小化理想情况下每个测试仅包含一个断言

Quick Reference

快速参考

I need to...UseReference
Build feature test-firstTDD (RED-GREEN-REFACTOR)tdd-cycle.md
Write unit/integration testsMocking, data strategies, patternsunit-integration.md
Test critical user flows in browserE2E with Playwrighte2e-playwright.md

我需要...采用方案参考文档
测试优先的方式开发功能TDD(红-绿-重构)tdd-cycle.md
编写单元/集成测试Mock、数据策略、测试模式unit-integration.md
在浏览器中测试核心用户流程基于Playwright的E2E测试e2e-playwright.md

Anti-Patterns (Universal)

反模式(通用)

❌ Don't✅ Do
Test implementation detailsTest observable behavior
Write tests after shippingWrite tests before/during
Duplicate test codeUse factories & fixtures
Complex test setupSimplify or split
Ignore flaky testsFix root cause
Skip cleanupReset state in teardown
Multiple asserts per testOne behavior per test

❌ 不要✅ 推荐
测试实现细节测试可观测的行为
上线后再补测试在开发前/开发过程中编写测试
重复编写测试代码使用工厂方法和fixture
测试设置逻辑复杂简化逻辑或者拆分测试
忽略不稳定的测试修复问题根因
跳过清理步骤在teardown阶段重置状态
单个测试包含多个断言每个测试仅验证一个行为

🔧 Runtime Scripts

🔧 运行脚本

ScriptPurposeCommand
scripts/test_runner.py
Unified test execution
python scripts/test_runner.py <project_path>
scripts/playwright_runner.py
Browser E2E testing
python scripts/playwright_runner.py <url>
With screenshot
python scripts/playwright_runner.py <url> --screenshot
Accessibility check
python scripts/playwright_runner.py <url> --a11y

Remember: The test is the specification. If you can't write a test for it, you don't understand the requirement.
脚本用途命令
scripts/test_runner.py
统一测试执行
python scripts/test_runner.py <project_path>
scripts/playwright_runner.py
浏览器E2E测试
python scripts/playwright_runner.py <url>
带截图功能
python scripts/playwright_runner.py <url> --screenshot
无障碍检查
python scripts/playwright_runner.py <url> --a11y

记住: 测试就是规范。如果你无法为某个需求编写测试,说明你还没有理解该需求。