Trung BìnhTesting iconTesting

Test pyramid là gì? Ba layers của test pyramid?

Test pyramid của Mike Cohn: base là nhiều unit tests (nhanh <1ms, rẻ, isolated, dễ maintain), giữa là integration tests (test interaction giữa components/services với real dependencies), đỉnh là ít E2E tests (chậm, đắt, brittle — dễ fail vì network, timing, UI changes).

  • Lý do balance 70/20/10: unit tests cho fast feedback và precise error location; integration tests catch boundary bugs unit tests miss; E2E tests verify user journeys nhưng cost cao.
  • Ice cream cone anti-pattern (inverted pyramid): nhiều E2E, ít unit — CI chạy chậm, flaky tests, khó debug khi fail.
  • Testing Trophy (Kent C.
  • Dodds): variation với nhiều integration tests hơn pyramid — 'integration tests give the most confidence per dollar of test cost'.
  • Service layer testing: test business logic through service/use-case layer với real database (integration) thay vì mock everything — catches more real bugs.
  • Practical: không nên có ratio cứng nhắc, phụ thuộc vào project.
  • Smoke tests: minimal E2E tests chạy sau mỗi deployment để verify critical paths hoạt động.
  • Contract tests thay E2E trong microservices.
  • Key insight: fast tests chạy thường xuyên → fast feedback → catch bugs sớm; slow E2E tests chạy ít hơn → feedback chậm.

Xem toàn bộ Testing cùng filter theo level & chủ đề con.

Mở danh sách Testing