TDD comes from Test-Driven Development, or test-driven development. The central rule is to write the test before the code it checks. It seems backwards, but it helps you think about what the code needs to do before you start typing.

How it works

TDD follows a short three-step cycle, known as red, green, refactor:

  1. Red: write a test for a functionality that does not yet exist. It will fail.
  2. Green: write the simplest possible code for the test to pass.
  3. Refactor: improve the code, without breaking the test.

Then you repeat the cycle for the next functionality. Each cycle takes a few minutes.

The result is a set of tests that grows with the project. If something breaks later, the test warns you right away.

Practical example

You need a function that calculates shipping. First write the test:

Python · tests/test_frete.py
from frete import calcular_frete

def test_frete_gratis_acima_de_200():
    assert calcular_frete(250) == 0

Running it, the test fails, because the function does not exist. Now write the minimum:

Python · frete.py
def calcular_frete(valor):
    if valor >= 200:
        return 0
    return 20

The test passes. Then you add another test, for example for values below 200, and repeat.

What it is for

TDD is used to avoid bugs, keep the code simpler and give safety to change the project. It also works as documentation: tests show how the code should be used.

With AI agents, TDD gains an extra role. You write the tests, or ask the agent to write them, and let the agent code until everything passes. Tests become an objective criterion for "done", which reduces code that looks right but does not work. Platforms like bridgecode, which coordinate several agents, benefit from this, because each agent can be validated by automated tests.

Difference between TDD and testing later

In the traditional method, you write the code and then create the tests, if there is time left. In TDD, the test comes first and guides the design of the code. Testing later tends to leave parts without coverage. TDD requires more discipline at the start, but saves time in maintenance.