← 返回规律列表
质量

测试金字塔

项目应该有大量快速单元测试,较少集成测试,以及少量端到端界面测试。

显示原始英文内容

Testing Pyramid

A project should have many fast unit tests, fewer integration tests, and only a small number of UI tests.

Takeaways

  • Unit testing is where you start. Unit tests are run as separate functions and execute quickly. That means you can afford to write lots of unit tests.
  • After the unit tests, there must be an integration test layer. These verify the integration of modules. You will require fewer integration tests than unit tests.
  • End-to-end tests at the top simulate real-world user scenarios. They are essential but slow and expensive to maintain.
  • Organizing tests this way, you receive quick feedback (most tests are quick unit tests), and when your UI test fails, there's a better chance it's due to a real problem.

Overview

The pyramid is a visual metaphor: the largest level (at the bottom) is unit tests, which are fast and the most numerous. The middle layer is integration tests, fewer in number. The top is UI/end-to-end tests, the least numerous. As you go up, tests become more expensive in time, effort, and fragility, so you want proportionally fewer of them.

By following the pyramid, most bugs are caught at the cheapest level. If the pyramid is inverted (lots of end-to-end tests, few unit tests), the test suite tends to be slow and fragile.

Examples

Consider a web e-commerce application. Following the test pyramid, the team writes extensive unit tests for functions such as price calculation, discount logic, and validation (hundreds of unit tests). They also write API integration tests verifying that order placement connects inventory, payment, and notification services correctly (a few dozen tests). Finally, they have a few end-to-end tests simulating user journeys like browsing, adding to cart, and checkout.

Because they have so many unit tests, if something in business logic breaks, it's caught before end-to-end tests run. This approach takes little time in CI and allows confident deployments.

Now consider a team that did not follow the pyramid. They have few unit tests and instead rely on 50 end-to-end GUI tests that run nightly for hours. They often find failing tests, but it's hard to tell if it's genuine bugs or test flakiness. Developers get feedback with a day's delay. This is the *anti-pattern* the pyramid warns against.

Origins

Mike Cohn is credited with popularizing the Test Pyramid. He described it in his book *Succeeding with Agile* and blog posts around 2009, coining the term and concept.

The idea builds on earlier testing theory (such as test levels in the ISTQB foundation or the general practice in TDD of writing many unit tests). Modern discussions sometimes add layers or modify terms, but the core principle remains: test more at the unit level than at the UI level.

核心含义

底层测试执行快、定位准、维护成本低,适合覆盖大量业务规则;越接近真实界面的测试越慢、越脆弱,但可以验证跨系统行为。因此测试组合通常应呈金字塔形,而不是把所有信心都押在 UI 自动化上。

测试层次不是绝对比例,重点是让反馈速度、覆盖范围和维护成本保持平衡。

实践例子

订单计算用大量单元测试覆盖,数据库边界用少量集成测试验证,关键购买流程再用少量浏览器测试覆盖。这样既能快速反馈,也能捕捉系统集成问题。

来源与边界

Mike Cohn 在《敏捷软件开发》中推广了测试金字塔。现代前端和分布式系统可能需要调整层次,但“越高层越昂贵、越少量”仍是有用的起点。