articles / tdd-ecrire-le-test-en-premier

TDD: why writing the test before the code changes everything

Mounir Guizani · 29 November 2026 · 4 min read

  • tdd
  • craftsmanship
  • tests
  • java

Writing the test before the code feels counter-intuitive. After ten years of banking projects where a regression is expensive, it is nonetheless the practice that has saved me the most time. Here is why, without dogma.

The principle in 30 seconds

TDD (Test-Driven Development) is a short loop, repeated dozens of times a day:

  1. Red: I write a small test describing the expected behaviour. It fails, since the code doesn't exist.
  2. Green: I write the minimum code to make it pass.
  3. Refactor: I clean up the code (and the test) while keeping everything green.

Each loop takes a few minutes. The test is not an after-the-fact check: it is the specification the code must satisfy.

A concrete example

A simple business rule: an instant transfer costs €1, a standard transfer is free.

class FeeCalculatorTest {

    private final FeeCalculator calculator = new FeeCalculator();

    @Test
    void an_instant_transfer_costs_one_euro() {
        var transfer = Transfer.instant(new BigDecimal("500"));
        assertEquals(new BigDecimal("1.00"), calculator.feeFor(transfer));
    }

    @Test
    void a_standard_transfer_is_free() {
        var transfer = Transfer.standard(new BigDecimal("500"));
        assertEquals(BigDecimal.ZERO, calculator.feeFor(transfer));
    }
}

Before writing a single line of production code, I already had to decide on the names (Transfer, FeeCalculator), the API (feeFor) and the cases to cover. That is where TDD works: it forces you to design from the point of view of the code's user.

TDD, tests written afterwards, manual tests: the real difference

Manual tests Tests written afterwards TDD
When the bug is found in acceptance, or even in prod a few days later within a minute
Effect on design none low: you test what exists strong: code is born testable
Coverage varies gaps on the "obvious" cases every behaviour has its test
Fear of change high medium low: the safety net is there
Upfront cost none medium higher in the first weeks

The key point: tests written afterwards test the code as it is, design flaws included. TDD does the opposite: it shapes the code so that it is easy to test, and therefore decoupled.

What I gain every day

  • Immediate feedback. I know within seconds if my change breaks something, not three days later in acceptance.
  • Fearless refactoring. On a legacy application, this safety net is what lets you modernise without breaking everything.
  • Executable documentation. an_instant_transfer_costs_one_euro says what the code does, and stays true as long as the test passes.
  • Small steps. You move forward in verified increments: less debugging, fewer big surprises.

Common objections

"It slows you down." At first, yes. But the time spent writing tests is won back on debugging, acceptance testing and production fixes. On a batch job handling millions of rows, a bug found in prod costs far more than ten tests.

"I don't have time to test everything." TDD doesn't ask you to test everything: it asks you to test what you write, when you write it.

"It doesn't work for everything." True. For a throwaway prototype or a UI exploration, I don't do TDD. For a business rule, a calculation or a data transformation: always.

TDD and BDD: complementary

TDD works at the code level (a class, a function). BDD works at the business behaviour level, in a language the business understands. In practice I use both: a BDD scenario describes the need, then TDD cycles build the code that satisfies it.

To start tomorrow

  1. Pick a simple business rule in your next task.
  2. Write a single failing test, with a name that reads like a sentence.
  3. Make it pass with the simplest possible code, then clean up.
  4. Start again with the next case.

TDD isn't decreed, it's practised. After two weeks, going back becomes hard.

Learned on a missionLCL Banque : Software EngineerSee the mission →