测试 Pyramid
一句话解释
测试 pyramid says: lots 的单元测试 (快, cheap), 一些 integration 测试 (中等), 少数 E2E 测试 (慢, expensive).
用大白话说
测试 pyramid 是指南 for 如何 distribute 你的测试. Picture pyramid 使用 three layers:
- Base (widest): 单元测试 — 许多的 它们. 它们're 快 (milliseconds), cheap 到编写 and maintain, and give 精确 feedback. 这个应该是 bulk 的你的测试.
- Middle: Integration 测试 — moderate 数字. 它们're slower and 测试多个组件 together. Use 它们到 验证模块边界.
- Top (narrowest): E2E 测试 — very 少数. 它们're 慢 (seconds), expensive, and brittle. Use 它们仅 for 关键用户 flows.
The pyramid shape reminds 你: 更多测试 at 底部, fewer at 顶部. 如果你的 pyramid 是 inverted (lots 的 E2E, 少数单元测试), 你'll 有慢, brittle 测试 suites 那个是 困难到 maintain.
示例
javascript
// Test Pyramid — relative test counts
//
// /// /E2E ← few, slow, brittle
// /------// / Integ ← moderate
// /----------// / Unit ← many, fast, isolated
// /----------------
// Typical distribution in a healthy project:
const testSuite = {
unit: 500, // 80% — fast, isolated
integration: 100, // 15% — real module interactions
e2e: 25, // 5% — critical user journeys only
};
const total = Object.values(testSuite).reduce((a, b) => a + b, 0);
console.log('Unit:', (testSuite.unit / total * 100).toFixed(0) + '%'); // 80%
console.log('E2E:', (testSuite.e2e / total * 100).toFixed(0) + '%'); // 4%测试金字塔指导测试分布:底层有大量快速的单元测试,中层是较少的集成测试,顶层是极少数慢速的 E2E 测试。这样保持测试套件快速且可靠。
相关术语
相关资源
相关课程
- 测试 Pyramid
Balancing 测试类型.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。