08. Shipping / Lesson 1 of 2
Testing
Find the bug before your users do, without waiting all day.
- 8 min
- Beginner
- 9 steps
- Tests0 (bad)
- Run time—
- Failure points tothe customer (bad)
- Request
- Response
- Failed request
- Health check
Step 1 of 9: Without tests, your users are the testers
Read every step as text
Step 1: Without tests, your users are the testers
A developer changes the tax code and the checkout total comes out wrong for one country. It looked fine on the developer's laptop. Nobody noticed until customers did.
A test is a small program that runs your code and checks the result. It turns "I think it works" into "I ran it, and it works", every time the code changes.
The later a bug is found, the more it costs: a customer's money, a support call, a night fix.
Step 2: Unit test: one small piece, no outside world
A unit test calls one function with chosen input and checks the output. It uses no network and no database, so it is very fast: say 2 ms each, so
1,000 × 2 ms = 2 s.When one fails, you know exactly which function broke. Write many of them, including the odd cases: zero, empty, negative, huge.
Fast tests get run all the time, on every save. Slow tests get skipped.
Step 3: Integration test: pieces working together
Each function can pass its unit tests and the system can still be broken: the SQL has a typo, the column has another name, the JSON has a different shape. An integration test runs real parts together, such as the app with a real test database.
It is slower, say 300 ms each:
100 × 300 ms = 30 s. Fewer of them are enough, because they check the joins between parts and not every detail.Give every test fresh data and clean up after it. Tests that share data fail in confusing ways.
Step 4: End-to-end test: act like a real user
An end-to-end (E2E) test drives a real browser through the real system: open the site, add to cart, pay (in a payment sandbox), see the receipt. It proves the whole journey works. Each takes seconds: say 15 s, so
20 × 15 s = 5 min.They are also flaky: a slow page or a changed button can fail a test when nothing is broken. And when one fails, the cause could be anywhere. Keep only the few journeys that must never break.
A flaky test teaches people to ignore red. Fix it or delete it.
Step 5: The test pyramid: many fast, few slow
Together the three kinds run 1,120 tests in about 5.5 min. Many unit tests at the bottom, fewer integration tests in the middle, a handful of E2E tests at the top.
Flip it and test everything through the browser: 1,120 E2E tests would take
1,120 × 15 s = 4.7 h. Nobody waits that long, so the tests stop being run.Test each thing at the lowest level that can catch it. Use the slow tests only for what the fast ones cannot see.
Step 6: Contract test: two services agree on an API
In a system of services, the Orders service calls the Payments service. Payments renames a field, and Orders breaks in production, though both teams passed their own tests.
A contract test writes down what the caller expects (for example
amountis a number). The Payments build checks that contract on every change, with no need to run both services together. They are quick: say 30 tests of 100 ms, 3 s in all.The contract is a shared promise. Breaking it fails the provider's build, before anyone deploys.
Step 7: Load test: how much can it take?
A correct system can still fall over under traffic. A load test sends many fake users at a copy of the system and raises the load step by step, while you watch the response time and error rate.
Here the 99th-percentile time stays near 120 ms up to 600 requests a second, then jumps past 2 seconds at 800. That is your limit, and the bottleneck shows where to fix. Run it on a production-like copy, never on the real system by accident.
Load tests answer "how many users can we take?", the capacity question from napkin math, with measurements instead of guesses.
Step 8: Chaos test: break it on purpose
Servers die, networks slow down. A chaos test causes those failures deliberately, in a controlled way, to check that the system copes: kill one of three servers, or add 300 ms to a database call.
State a hypothesis first ("errors stay below 1% and the page still loads"), start small, have a stop switch, and watch the dashboards. If the hypothesis fails, you found a weak spot on your own schedule instead of at 3 a.m.
Begin in a test environment or with a tiny share of traffic. Chaos is an experiment, not a surprise.
Step 9: Testing in one picture
Our suite has 1,150 tests and runs in about 5.6 min: unit tests for the details, integration tests for the joins, a few E2E journeys, and contract tests between services.
Load tests find the limit; chaos tests check the system survives failures. Keep the pyramid the right way up, and keep every test fast and trustworthy.