Testing strategy that scales
Which tests to write at each level, how to keep a growing suite fast and trusted, and how to spot tests that check nothing.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
A team runs an online shop. Its main test suite clicks through the site in a real browser: sign in, search, add to cart, pay. There are 300 of these tests, and the suite takes 40 minutes.
Every week a few tests fail with no change to the code. A timeout here, a slow page there. The team learns a habit: when a test fails, press "re-run". Usually it turns green.
One Friday, the payment test fails. A developer re-runs it twice, and on the third run it passes. He merges. The failure was real: a race in the new payment code that showed up only some of the time. Customers were charged twice over the weekend.
This story is made up. But Google has seen the same thing: developers dismiss a failure as flaky and later find it was a real bug. 4 The tests were not the problem. The strategy was.
The idea in simple words
Tests are not there to find bugs once. They are there so you can change code with confidence, again and again, for years. 1 A good suite tells you fast, and truthfully, if your change broke something.
Tests come in three common scopes:
- Unit tests check one small piece, like a function or a class.
- Integration tests check a few parts working together, like your code and a real database.
- End-to-end tests run the whole system the way a user does. 1
Lower tests are fast and cheap, and a failure points close to the cause. 1 Tests through the UI are slow, brittle and expensive to write, but they see the real system. 5 A strategy is the mix you choose, and the rules that keep it fast and trusted.
The key idea. Write many small, fast tests and only a few large ones. In our view, a test that people do not trust does harm, because it teaches them to ignore red.
How it works
Step through one change as it passes four layers of checks. Use Next and Back, or the ← and → keys.
- In this example, a developer changes how discounts work. Unit tests run first, on her laptop and in CI. Here they are also what Google calls small tests: one process, no network, a 60-second limit. One catches a rounding bug.
- Integration tests run the code with a real database on the same machine. In Google's terms these are medium tests, with a 300-second limit. A unit test with a fake database could not see that the SQL names a column that does not exist.
- A few end-to-end tests click through checkout in a real browser, against the whole system. They are slow and can be flaky, so you keep them few. One finds that the page does not show the discount.
- After the merge, the change goes to a canary: a small share of servers with real users. Canary analysis compares its health with the rest of production. A query that was fast on test data is slow on real data. Latency goes up on the canary, so the rollout stops and rolls back.
- The bugs here are examples. Higher layers are slower and cost more, but they see more of the real system. Push each check as low as it can go, and keep the top layers small.
The test pyramid. Mike Cohn made the pyramid popular in his 2009 book Succeeding with Agile. The bottom is many unit tests. The top is a few tests through the UI. 5 Ham Vocke sums it up in two rules: write tests at different levels, and the higher the level, the fewer tests you should have. 6 As a very rough guide, Google aims for about 80% unit, 15% integration and 5% end-to-end tests. 1
The opposite shape is the ice cream cone: many end-to-end tests and few unit tests. Google says such suites tend to be slow, unreliable and hard to work with. 1 The team in the story had an ice cream cone.
The critics: the testing trophy. Kent C. Dodds draws a trophy instead. From the bottom, it has static checks, unit tests, integration tests and end-to-end tests. He builds on Guillermo Rauch's advice: "Write tests. Not too many. Mostly integration." His rule: "The more your tests resemble the way your software is used, the more confidence they can give you." 7 He made it for JavaScript apps, mostly in one codebase. 7 In our view, both shapes agree on the main point: keep end-to-end tests few. They differ on how much weight integration tests get.
Size is not the same as scope. "Unit" and "integration" mean different things to different teams. So Google also sorts tests by the resources they use. 3
- Small: one process, no network, no disk, no sleep. Time limit 60 seconds.
- Medium: one machine. Network to localhost only, so a database in a container is fine. Limit 300 seconds.
- Large: anything, across many machines. Limit 900 seconds or more. 3 1
Google cares most about speed and determinism, and size predicts both. 1 A test that cannot touch the network cannot fail because the network was slow.
Flaky tests. A flaky test passes and fails on the same code. In 2016, Google reported that about 1.5% of all its test runs gave a flaky result. Almost 16% of its tests had some flakiness. 4 In post-submit CI, about 84% of pass-to-fail changes involved a flaky test. 4 The book says tests begin to lose value as flakiness nears 1%. 1
The real cost is trust. People ignore alarms that are often false, so real failures get dismissed. 4 Google's tools re-run failing tests and quarantine very flaky ones. Quarantine has a cost: it can hide a real race in the code. 4 Treat a flaky test as a bug with an owner, not as noise.
Test data. Large tests need data that looks real. Data written by hand covers a narrow range of cases and reflects the author's biases. Copied production data is more real, and a sample of it keeps the size down. 2 Google's book says tests should not depend on the order they run in, or on a shared database. 1 So let each test create its own data.
Contract tests between services. End-to-end tests across many services are slow and brittle. A contract test checks each service alone against a shared contract. 8 In Pact, the consumer's tests generate the contract, so it holds only the requests the consumer really uses. The provider then checks that it can answer them. 8 6
Testing in production. Some bugs appear only with real traffic, real data and real config. Google's book says config changes are the number one cause of its major outages. Unit tests alone cannot check them. 2 So the last check runs in production. A prober is a test that checks production, usually with read-only actions. During a staged rollout, canary analysis compares the canary's health with the rest of the fleet. 2 See Safe deploys and config changes for how a canary stops a bad release.
Mutation testing. Line coverage only shows which code ran during tests. It does not show that a test would notice a bug. Mutation testing makes small changes to your code, such as flipping a condition, and runs the tests again. If a test fails, the change is "killed". If all tests still pass, the change "lived", and your tests missed it. 9 It runs your tests many times, once per change. So we suggest you start with the code that matters most. See Property-based and mutation testing.
What to test first. You cannot test everything, so start with risk. Ask: what costs the most if it breaks, and what changes most often? Money, auth, data loss and shared libraries come first. Google calls this the Beyoncé Rule: test everything you do not want to break. In its words, "If you liked it, then you shoulda put a test on it." 1
Trade-offs
| Option | Speed | What it catches | What it misses | Keep it |
|---|---|---|---|---|
| Unit (small) | Fast. Run them on every change. | Logic, edge cases, rounding. Points close to the cause. | Wrong SQL, config, how parts fit together. | Many. Most of the suite. 1 |
| Integration (medium) | Slower. Limit 300 s per test. 3 | Real database, queues, serialisation. | Other teams' services, the full user flow. | A good number, for each boundary you own. |
| Contract | Each side runs alone, without the full system. 8 | Broken requests or responses between services. | Whether the whole flow works for a user. | One per consumer and provider pair. |
| End-to-end (large) | Slow. Often flaky. 6 | A broken user journey across the system. | Exact cause of a failure. | Few. Only key journeys, like sign-up and pay. |
| Canary in production | Slow. It waits for real traffic. | Real data, real load, real config. 2 | Bugs that hit only rare users. Some users see errors. | On every deploy. |
Simple rule: test each thing at the lowest level that can catch it. When a higher test finds a bug, add a lower test for it. 6
At work: how a senior uses this
- Ask in design reviews: "How will we know this works, and how fast?" Plan the tests with the feature.
- Watch suite time. Ask: "How long from push to green?" If it grows, move tests down the pyramid.
- Track flaky tests on a list with owners. Fix or delete them. Do not let "re-run" become a habit.
- Use a real database in a container for data code. Use a test double for slow or paid services, like a payment provider.
- When a bug reaches production, write the lowest test that would have caught it, before you fix it.
- In reviews, read the assertions first. Ask: "If I broke this code, would this test fail?"
With AI
What to ask the agent
"Write unit tests for applyDiscount. Cover zero, negative, rounding and the maximum value. Every test must assert the exact result."
"Here is our test suite. Which tests are end-to-end checks of logic that a unit test could cover? List them with a lower-level replacement."
"This test is flaky. Read it and list every source of non-determinism: time, order, randomness, network and shared state."
What to check yourself
- Every test has an assertion that would fail if the code were wrong. AI tools sometimes write tests that only call the code, or assert that a mock was called. See Reviewing human and AI code.
- The test does not copy the code's logic into the expected value. Then a wrong formula passes.
- Break the code on purpose, or run a mutation tool. If the tests stay green, they test nothing.
- No
sleepcalls, real clocks or real network in unit tests. - The agent did not change or delete a failing test to make the build green.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 9 sources and related lessons
- Adam Bender, “Testing Overview”, chapter 11 of Software Engineering at Google (O'Reilly, 2020) (opens in a new tab)
Why we test, test size against test scope, the 80/15/5 mix, the ice cream cone, and the cost of flaky tests. - Joseph Graves, “Larger Testing”, chapter 14 of Software Engineering at Google (O'Reilly, 2020) (opens in a new tab)
What unit tests cannot see, test data, probers and canary analysis in production. - Simon Stewart, “Test Sizes”, Google Testing Blog, 2010 (opens in a new tab)
A table of what small, medium and large tests may do, with time limits. - John Micco, “Flaky Tests at Google and How We Mitigate Them”, Google Testing Blog, 2016 (opens in a new tab)
How often tests flake at Google, why it costs so much, and re-runs and quarantine. - Martin Fowler, “TestPyramid”, martinfowler.com, 2012 (opens in a new tab)
Where the test pyramid comes from, and why tests through the UI are slow and brittle. - Ham Vocke, “The Practical Test Pyramid”, martinfowler.com, 2018 (opens in a new tab)
The two rules of the pyramid, consumer-driven contract tests with Pact, and why end-to-end tests should be few. - Kent C. Dodds, “The Testing Trophy and Testing Classifications”, 2021 (opens in a new tab)
The testing trophy, and the idea that tests should resemble how the software is used. - Pact documentation, “Introduction” (opens in a new tab)
What contract testing is, and how a contract is generated from the consumer's tests. - PIT mutation testing for Java, pitest.org (opens in a new tab)
A short, clear explanation of mutation testing and why line coverage is not enough.
Related: Flaky tests and test reliability (coming soon) · Contract and integration tests for services (coming soon) · Property-based and mutation testing (coming soon) · CI/CD and trunk-based development · Safe deploys and config changes · Reviewing code written by humans and AI
- Flaky tests: finding and fixing the causes.
- Contract and integration tests: Pact and testing service boundaries.
- CI/CD and trunk-based development: where these tests run.