testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Strategy — The Test Trophy
测试策略 — Test Trophy模型
Overview
概述
The Test Trophy (Kent C. Dodds) is a modern testing strategy that prioritizes integration tests over unit tests, reflecting how today's tooling has shifted the cost/confidence tradeoffs of each test category.
"Write tests. Not too many. Mostly integration." — Kent C. Dodds
Test Trophy(由Kent C. Dodds提出)是一种现代测试策略,它将集成测试置于比单元测试更优先的位置,反映了当前工具链如何改变了各类测试的成本与可信度权衡关系。
"编写测试,但不要写太多。主要写集成测试。" — Kent C. Dodds
The Test Trophy
Test Trophy模型
┌───────┐
│ E2E │ Few, high-confidence, slow, expensive
─┴───────┴─
┌─────────────┐
│ Integration │ Most tests here — best ROI
─┴─────────────┴─
┌───────────────────┐
│ Unit │ Isolated logic, fast, cheap
─┴───────────────────┴─
┌───────────────────────────┐
│ Static Analysis │ Free — catches bugs at write time
└───────────────────────────┘ ┌───────┐
│ E2E │ 数量少、可信度高、速度慢、成本高
─┴───────┴─
┌─────────────┐
│ Integration │ 测试数量最多 — 投资回报率最佳
─┴─────────────┴─
┌───────────────────┐
│ Unit │ 孤立逻辑测试、速度快、成本低
─┴───────────────────┴─
┌───────────────────────────┐
│ Static Analysis │ 零成本 — 在编写代码时捕获bug
└───────────────────────────┘Test Categories
测试分类
| Category | What It Tests | Speed | Confidence | Cost |
|---|---|---|---|---|
| Static Analysis | Types, syntax, lint rules, security patterns | Instant | Low-Medium | Free |
| Unit | Individual functions/classes in isolation | Fast | Medium | Low |
| Integration | Multiple units working together | Medium | High | Medium |
| E2E | Full user flows through the real system | Slow | Very High | High |
| 分类 | 测试内容 | 速度 | 可信度 | 成本 |
|---|---|---|---|---|
| Static Analysis | 类型、语法、代码规范、安全模式 | 即时 | 中低 | 免费 |
| Unit | 孤立的单个函数/类 | 快 | 中等 | 低 |
| Integration | 多个单元协同工作的场景 | 中等 | 高 | 中等 |
| E2E | 真实系统中的完整用户流程 | 慢 | 极高 | 高 |
Beyond the Trophy
扩展分类
| Category | What It Tests | When to Use |
|---|---|---|
| Contract | API compatibility between services | Microservices / distributed systems |
| API | HTTP endpoints directly | REST/GraphQL APIs |
| Performance | Load, stress, scalability | Before launch, capacity planning |
| Visual | UI appearance / regression | Design systems, component libraries |
| Accessibility | WCAG compliance, screen readers | All user-facing apps |
| Acceptance | Business requirements (BDD) | Stakeholder-facing features |
| Chaos | System resilience under failure | Distributed systems, microservices |
| 分类 | 测试内容 | 使用场景 |
|---|---|---|
| Contract | 服务间的API兼容性 | 微服务/分布式系统 |
| API | 直接测试HTTP端点 | REST/GraphQL API |
| Performance | 负载、压力、可扩展性 | 上线前、容量规划 |
| Visual | UI外观/回归测试 | 设计系统、组件库 |
| Accessibility | WCAG合规性、屏幕阅读器适配 | 所有面向用户的应用 |
| Acceptance | 业务需求(BDD) | 面向利益相关者的功能 |
| Chaos | 故障下的系统韧性 | 分布式系统、微服务 |
Right-Sizing Your Tests
合理规划测试
The Trophy Distribution
测试占比分配
Allocate effort roughly proportional to the Trophy shape:
| Category | % of Tests | Rationale |
|---|---|---|
| Static Analysis | Always on | Zero-cost, catches trivial bugs |
| Unit | ~20% | Pure logic, algorithms, edge cases |
| Integration | ~50% | Best confidence-per-dollar |
| E2E | ~20% | Critical user journeys only |
| Other (contract, perf, a11y, visual) | ~10% | As needed per project type |
按照Test Trophy模型的形状大致分配精力:
| 分类 | 测试占比 | 理由 |
|---|---|---|
| Static Analysis | 始终启用 | 零成本,捕获低级bug |
| Unit | ~20% | 纯逻辑、算法、边缘场景 |
| Integration | ~50% | 每单位成本的可信度最高 |
| E2E | ~20% | 仅针对关键用户流程 |
| 其他(contract、perf、a11y、visual) | ~10% | 根据项目类型按需分配 |
When to Shift the Balance
调整测试平衡的场景
| Project Type | Emphasize | Reduce |
|---|---|---|
| Library / SDK | Unit tests (public API surface) | E2E (no UI) |
| Microservices | Contract + integration | E2E (too many services) |
| Monolithic web app | Integration + E2E | Contract (single deploy) |
| Design system | Visual + accessibility | Performance |
| Real-time / trading | Performance + unit | Visual |
| Regulated / healthcare | Acceptance (BDD) + integration | — |
| 项目类型 | 重点关注 | 减少投入 |
|---|---|---|
| 库/SDK | 单元测试(公共API层面) | E2E测试(无UI场景) |
| 微服务 | Contract测试 + 集成测试 | E2E测试(服务数量过多) |
| 单体Web应用 | 集成测试 + E2E测试 | Contract测试(单一部署单元) |
| 设计系统 | Visual测试 + 可访问性测试 | 性能测试 |
| 实时/交易系统 | 性能测试 + 单元测试 | Visual测试 |
| 受监管/医疗系统 | 验收测试(BDD) + 集成测试 | — |
Cross-Platform Tool Landscape
跨平台工具生态
Static Analysis
Static Analysis
| Tool | Languages |
|---|---|
| TypeScript | JS/TS (type checking) |
| ESLint / Biome | JS/TS (linting) |
| Roslyn Analyzers | C# |
| Pylint / Ruff / mypy | Python |
| Semgrep | Multi-language SAST |
| 工具 | 支持语言 |
|---|---|
| TypeScript | JS/TS(类型检查) |
| ESLint / Biome | JS/TS(代码规范检查) |
| Roslyn Analyzers | C# |
| Pylint / Ruff / mypy | Python |
| Semgrep | 多语言SAST |
Unit Testing
Unit Testing
| Tool | Languages |
|---|---|
| Vitest / Jest | JS/TS |
| xUnit / NUnit / MSTest | C# |
| pytest | Python |
| JUnit / TestNG | Java |
| Go test | Go |
| 工具 | 支持语言 |
|---|---|
| Vitest / Jest | JS/TS |
| xUnit / NUnit / MSTest | C# |
| pytest | Python |
| JUnit / TestNG | Java |
| Go test | Go |
Integration Testing
Integration Testing
| Tool | Languages |
|---|---|
| Testcontainers | Java, .NET, Node, Python, Go |
| Vitest / Jest | JS/TS |
| xUnit + WebApplicationFactory | C# / ASP.NET |
| pytest | Python |
| 工具 | 支持语言 |
|---|---|
| Testcontainers | Java, .NET, Node, Python, Go |
| Vitest / Jest | JS/TS |
| xUnit + WebApplicationFactory | C# / ASP.NET |
| pytest | Python |
E2E Testing
E2E Testing
| Tool | Platforms |
|---|---|
| Playwright | Web (Chromium, Firefox, WebKit) |
| Cypress | Web (Chromium-based) |
| Selenium | Web (all browsers) |
| Appium | Mobile (iOS, Android) |
| Maestro | Mobile (iOS, Android) |
| 工具 | 支持平台 |
|---|---|
| Playwright | Web(Chromium, Firefox, WebKit) |
| Cypress | Web(基于Chromium) |
| Selenium | Web(所有浏览器) |
| Appium | Mobile(iOS, Android) |
| Maestro | Mobile(iOS, Android) |
Contract Testing
Contract Testing
| Tool | Type |
|---|---|
| Pact | Consumer-driven contracts |
| PactFlow | Bi-directional contract testing |
| Spring Cloud Contract | JVM contract testing |
| 工具 | 类型 |
|---|---|
| Pact | 消费者驱动的契约 |
| PactFlow | 双向契约测试 |
| Spring Cloud Contract | JVM契约测试 |
API Testing
API Testing
| Tool | Format |
|---|---|
| .http files | VS Code / JetBrains REST Client |
| Bruno | Git-friendly API collections |
| Postman / Newman | Collections + CLI runner |
| REST Client (VS Code) | Inline .http files |
| k6 | Scripted API + load testing |
| 工具 | 格式 |
|---|---|
| .http files | VS Code / JetBrains REST Client |
| Bruno | 支持Git的API集合 |
| Postman / Newman | 集合 + CLI运行器 |
| REST Client (VS Code) | 内嵌.http文件 |
| k6 | 脚本化API + 负载测试 |
Performance Testing
Performance Testing
| Tool | Type |
|---|---|
| k6 (Grafana) | Load / stress (JS scripts) |
| JMeter | Load / stress (GUI + CLI) |
| Gatling | Load / stress (Scala/Java) |
| Artillery | Load / stress (YAML config) |
| Lighthouse | Web performance audit |
| 工具 | 类型 |
|---|---|
| k6 (Grafana) | 负载/压力测试(JS脚本) |
| JMeter | 负载/压力测试(GUI + CLI) |
| Gatling | 负载/压力测试(Scala/Java) |
| Artillery | 负载/压力测试(YAML配置) |
| Lighthouse | Web性能审计 |
Visual Testing
Visual Testing
| Tool | Integration |
|---|---|
| Chromatic | Storybook visual regression |
| Percy (BrowserStack) | Cross-browser visual diffs |
| BackstopJS | CSS regression (headless) |
| Playwright screenshots | Custom visual assertions |
| 工具 | 集成方式 |
|---|---|
| Chromatic | Storybook视觉回归测试 |
| Percy (BrowserStack) | 跨浏览器视觉差异对比 |
| BackstopJS | CSS回归测试(无头模式) |
| Playwright screenshots | 自定义视觉断言 |
Accessibility Testing
Accessibility Testing
| Tool | Type |
|---|---|
| axe-core / @axe-core/playwright | Automated WCAG checks |
| Pa11y | CLI accessibility audits |
| Lighthouse | Accessibility scoring |
| Storybook addon-a11y | Component-level checks |
| 工具 | 类型 |
|---|---|
| axe-core / @axe-core/playwright | 自动化WCAG检查 |
| Pa11y | CLI可访问性审计 |
| Lighthouse | 可访问性评分 |
| Storybook addon-a11y | 组件级检查 |
Acceptance Testing (BDD)
Acceptance Testing (BDD)
| Tool | Languages |
|---|---|
| Cucumber | Java, JS, Ruby |
| SpecFlow / Reqnroll | C# |
| Behave | Python |
| Gauge | Multi-language (Markdown specs) |
| Godog | Go |
| 工具 | 支持语言 |
|---|---|
| Cucumber | Java, JS, Ruby |
| SpecFlow / Reqnroll | C# |
| Behave | Python |
| Gauge | 多语言(Markdown规范) |
| Godog | Go |
Chaos Testing
Chaos Testing
| Tool | Type |
|---|---|
| Chaos Monkey | Random VM termination (Netflix) |
| Gremlin | SaaS fault injection platform |
| Litmus | Kubernetes chaos engineering (CNCF) |
| Chaos Mesh | Kubernetes fault injection |
| Toxiproxy | TCP proxy for network faults |
| AWS Fault Injection Service | AWS-native chaos |
| Azure Chaos Studio | Azure-native chaos |
| 工具 | 类型 |
|---|---|
| Chaos Monkey | 随机VM终止(Netflix) |
| Gremlin | SaaS故障注入平台 |
| Litmus | Kubernetes混沌工程(CNCF) |
| Chaos Mesh | Kubernetes故障注入 |
| Toxiproxy | 网络故障TCP代理 |
| AWS Fault Injection Service | AWS原生混沌测试 |
| Azure Chaos Studio | Azure原生混沌测试 |
Test Automation Architecture
测试自动化架构
CI Pipeline
│
├── Static Analysis ──► ESLint + TypeScript + Semgrep (on every commit)
│
├── Unit Tests ────────► Vitest / xUnit / pytest (on every commit)
│
├── Integration Tests ─► Testcontainers + API tests (on every PR)
│
├── Contract Tests ────► Pact verify (on every PR)
│
├── E2E Tests ─────────► Playwright critical paths (on merge to main)
│
├── Visual Tests ──────► Chromatic snapshot comparison (on every PR)
│
├── A11y Tests ────────► axe-core in Playwright (on every PR)
│
├── Performance Tests ─► k6 load tests (nightly / pre-release)
│
└── Chaos Tests ────────► Litmus / Gremlin experiments (pre-release / game days)CI Pipeline
│
├── Static Analysis ──► ESLint + TypeScript + Semgrep(每次提交时运行)
│
├── Unit Tests ────────► Vitest / xUnit / pytest(每次提交时运行)
│
├── Integration Tests ─► Testcontainers + API测试(每次PR时运行)
│
├── Contract Tests ────► Pact verify(每次PR时运行)
│
├── E2E Tests ─────────► Playwright关键路径测试(合并到主分支时运行)
│
├── Visual Tests ──────► Chromatic快照对比(每次PR时运行)
│
├── A11y Tests ────────► axe-core in Playwright(每次PR时运行)
│
├── Performance Tests ─► k6负载测试(夜间/预发布时运行)
│
└── Chaos Tests ────────► Litmus / Gremlin实验(预发布/故障演练日运行)Best Practices
最佳实践
- Follow the Test Trophy shape — invest most in integration tests, not unit tests.
- Run static analysis on every keystroke (IDE) and every commit (CI) — it's free confidence.
- Write E2E tests only for critical user journeys — they're expensive to maintain.
- Use contract tests instead of E2E for verifying service boundaries in microservices.
- Use Testcontainers for integration tests that need real databases, message brokers, or caches.
- Use .http files or Bruno for API testing that's version-controlled alongside the code.
- Run performance tests regularly (nightly or pre-release), not just before launch.
- Include accessibility testing in CI — axe-core catches >50% of WCAG violations automatically.
- Use BDD/Gherkin for features where business stakeholders need to verify acceptance criteria.
- Keep test data factories close to the tests — avoid shared global test fixtures.
- Use chaos engineering to verify resilience assumptions — inject real faults in staging and production with controlled blast radius.
- 遵循Test Trophy模型的比例 — 重点投资集成测试,而非单元测试。
- 在IDE中实时运行静态分析,并在每次CI提交时运行 — 这是零成本的可信度提升手段。
- 仅为关键用户流程编写E2E测试 — 这类测试维护成本极高。
- 在微服务中,使用契约测试替代E2E测试来验证服务边界。
- 对于需要真实数据库、消息队列或缓存的集成测试,使用Testcontainers。
- 使用.http文件或Bruno进行API测试,确保测试与代码一起版本控制。
- 定期运行性能测试(夜间或预发布时),而不仅仅是上线前。
- 在CI中加入可访问性测试 — axe-core可自动捕获超过50%的WCAG违规问题。
- 对于需要利益相关者验证验收标准的功能,使用BDD/Gherkin。
- 测试数据工厂应贴近测试代码 — 避免使用共享的全局测试夹具。
- 使用混沌工程验证系统韧性假设 — 在预发布和生产环境中注入可控的真实故障。