Skip to content
BENIBIX Learn

Test-Driven Development (TDD)

Development🧪 Testing & QualityTest Practices

Also known as TDD, Test-First Development, Red Green Refactor

Write a test that fails first. Then write just enough code to pass.

Think of it like

Think of it like writing the quiz before you study. You know the goal before you start.

Example

Emma needs a function that rounds prices. She writes a test that 2.345 becomes 2.35, sees it fail, then writes the function until it passes.

Why it matters

Every line of code comes with a test. You also think about how code is used before you write it.

Definition

Test-driven development means you write a test before the code it checks. First you write a test and watch it fail, because the code does not exist yet. Then you write just enough code to make it pass. Then you tidy the code while the test stays green. This loop is called red, green, refactor, and Kent Beck made it popular around 2000.

Common mistake

Skipping the step where the test fails. A test you never saw fail might not be testing anything.

Key takeaways

  1. 1
    Red. Write a test that fails.
  2. 2
    Green. Write code until it passes.
  3. 3
    Refactor. Tidy, keep it green.

Compare