Back to blog

How do you ship every week without breaking the feed?

Thắng Đoàn
Thắng Đoàn

FotoFlow's checkout now applies discount codes, and a developer changed how totals are rounded. Before shipping, they click through a few purchases on a test server and it looks fine. Nobody tried three items at 9.99 with a 10 percent code; the new rounding charges one cent too much, and thousands of orders are wrong before support notices. Manual checks only cover what someone thought to try, and users found this bug.

Unit tests, with doubles for the rest

A unit test calls one function with known inputs, the basket and the discount code, and checks the output. It runs in milliseconds with no database and no network, so the team keeps thousands of them and they run on every change. That rounding case becomes one line that fails the moment anyone breaks it again, and the rule for every future bug: write the failing test first, then fix the code.

Checkout cannot run without its dependencies, so tests swap them for test doubles: an in-memory database, a payment stub that always approves, a mock that records the card was charged exactly once. Fast and repeatable, but a double only knows what you told it. If the real provider renames a field, every unit test still passes. Something has to test against the real thing too.

Integration, contract, end-to-end

An integration test runs Checkout against a real PostgreSQL started fresh in a container; SQL typos, broken migrations and mismatched types surface here, not in production. Between teams, a contract test records what Checkout expects from Inventory's API, and Inventory's own pipeline checks every change against it, so a rename fails in their build before it ships. On top, a handful of end-to-end tests drive the real system like a user: browse, add three items, pay with a sandbox card. They are the slowest and most fragile, so a dozen for the journeys that make money is plenty.

Together they form the test pyramid: thousands of unit tests at the bottom, hundreds of integration and contract tests in the middle, a few end-to-end tests on top. Push every check as low in the pyramid as it can go. The upside-down pyramid, mostly end-to-end, is a common trap: an hour per run, random failures, and a team that stops trusting red builds.

What this does not solve

One end-to-end test fails about one run in twenty because it waits a fixed two seconds for a confirmation email that sometimes takes three. People learn to press re-run, and when a real bug turns it red, they re-run that too. A flaky test is worse than no test: it trains the team to ignore failures. Fix the cause: wait for a condition instead of a fixed time, control the clock, give each test its own data. And tests prove what you thought to check. A load test finds where latency turns upward before users do, failure tests stop a replica on purpose to check failover, and the last layer runs in production itself: release to a few users first and watch the numbers, the canary in the next part. The later a bug is found, the more it costs.

Your last production bug: which layer of the pyramid would have caught it, and why did it not exist yet?
One test fails one run in twenty on your board right now. What is the team's habit when it goes red?

Tests answer "what else did this break." Next: running them on every change and getting it to production in an hour. Next in the series: From commit to production in one hour: what has to be true?

Sources

Martin Fowler: Test Pyramid

Google SRE Books

Martin Fowler: Continuous Integration

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.