"All the tests pass" is a sentence that only means something if the tests say something. Test-driven development (TDD) exists for this reason: a test that has never failed has never shown it can fail, so it proves nothing when it passes.
The cycle
Red. You write a test for the wanted behaviour and run it: it must fail, for the right reason. A test that passes at the first attempt may not prove what you think it does.
Green. You write the minimum code that makes it pass, nothing more.
Refactor. With the tests green you improve the structure (names, duplication, clarity) without changing behaviour. If a test breaks, the change altered something it should not have.
The proof for bugs
When a bug report arrives you do not start from the code: you start from a test that reproduces the bug and fails. Only then do you fix it. At the end the test passes and stays as protection: the same error will not come back silently. A fix without a reproduction test is an opinion.
The test pyramid
Many small, fast tests (unit tests), fewer integration tests, very few complete end-to-end tests. You choose them by the resource they consume: tests that touch neither network nor database are quick and run constantly; those that involve external services are slow and should be kept for what cannot be tried otherwise.
Writing good tests
- Check state, not interactions. A test should say "after the operation the result is X", not "function Y was called three times": the second breaks on every refactoring.
- Clarity before non-repetition. In tests a little repetition is fine if it makes each test readable on its own.
- Real implementations over fakes. Substitutes (mocks) are for expensive or non-deterministic boundaries; used everywhere, the tests prove the substitutes, not the code.
- Arrange, act, assert. Three distinct blocks in every test.
- One concept per test. When it fails, you must see at once what.
- Descriptive names. "rejects_the_booking_if_the_room_is_taken" says more than "test_booking_2".
Anti-patterns
Tests that check the framework's behaviour instead of the application's; tests that depend on execution order; tests skipped to make the suite pass; tests with names that describe nothing.
Warning signs
- Code written with no corresponding test.
- Tests that pass at the first attempt.
- "All the tests pass" when no test was actually run.
- Bug fixes without a reproduction test.
- Tests disabled to turn the suite green.
An example
A report: "the order total is wrong with two discounts". You write a test with one order and two discounts, which fails with the wrong total. You find that the second discount is applied to the original price instead of the reduced one. You fix it: the test passes. A month later, someone changes the calculation and the test fails before the defect reaches production.
When to use it
When implementing any logic, fixing a bug or changing a behaviour; when you need to show that the code works; when you are about to modify something that exists.
This method derives from the open-source agent-skills family by Addy Osmani (MIT licence), rewritten and adapted.
This article is an OIAR skill
What you have just read is the content of a skill from the OIAR catalogue, written for people. Knowledge like this, but richer and deeper, written for the agents, drives the interviews, the analysis and the report inside the platform. Skills are used in OIAR: to try this one on a project of yours you need an account.
test-driven-developmentA method for developing driven by tests: the red-green-refactor cycle, a reproduction test before fixing a bug, the test pyramid, rules for readable tests and anti-patterns. Use it when implementing logic, fixing a bug or changing existing behaviour.