A testing strategy that works: the pyramid, TDD and beyond
How many tests of which kind, how to write tests that catch real bugs, and how AI changes testing.
Key takeaways
- Many fast unit tests, fewer integration tests, very few end-to-end tests.
- Test behavior, not implementation details.
- A test that never fails is not protecting you. Check that tests can fail.
Tests are how a team moves fast without fear. A good test suite lets you refactor, upgrade and deploy on a Friday afternoon. A bad one is slow, flaky and ignored.
#The test pyramid
- Unit tests (base): test one function or class in isolation. Milliseconds each. Hundreds or thousands of them.
- Integration tests (middle): test real interactions, such as with a database, a queue or an HTTP API. Seconds each.
- End-to-end tests (top): drive the real UI through key user journeys. Slow and more brittle, so keep a handful for critical paths.
Note: Some teams prefer a "testing trophy" with more integration tests. The principle is the same: most confidence for the least time and maintenance.
#What makes a good test
- Tests behavior, not implementation. Assert on outputs and effects, not private method calls.
- Readable: Arrange → Act → Assert, with a name that states the rule.
- Deterministic: no reliance on time, randomness or test order.
- Fast: slow suites stop being run.
test("members with 5+ years get 15% discount", () => {
const user = aUser({ isMember: true, years: 6 });
expect(discount(user)).toBe(0.15);
});
#TDD in one minute
Red → Green → Refactor. Write a failing test, make it pass with the simplest code, then clean up. TDD isn't required everywhere, but it's excellent for business rules and bug fixes: first write a test that reproduces the bug.
#Flaky tests are bugs
A test that sometimes fails trains everyone to ignore failures. Quarantine flaky tests immediately, fix them within days, or delete them.
#Testing in the AI era
AI is great at generating tests, and great at generating useless tests.
- Danger: tests that just mirror the current implementation, so they pass even when the behavior is wrong.
- Fix: write the test names and cases yourself (the what) and let AI fill in the code (the how).
- Check: temporarily break the code. Do the tests fail? Mutation testing tools automate this.
AI tip: Prompt "Write tests for these acceptance criteria, including edge cases for empty, null, boundary and error inputs" rather than "write tests for this function".
#A practical target
- Unit tests run in under a minute.
- Full CI under 10–15 minutes.
- Every bug fix comes with a test.
- Critical user journeys covered end-to-end.
Keep reading
A practical, layered defense so that faster code generation doesn't mean more production incidents.
How to make code reviews fast, kind and genuinely useful, including reviews of AI-generated code.
Five principles for code that is easy to change, each explained with a short example and a warning about overuse.