How do you ship every week without breaking the feed?
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
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.