Show in graph

Software → quality-assurance

TDD

A development practice where tests are written before implementation code to guide design and behavior.

Motivation

Test-Driven Development solves the problem of writing code without a clear, executable definition of correct behavior.

Where it fits

TDD belongs to software testing and design. The goal is not just tests; it is feedback that shapes simpler, more testable code.

Flow

  1. Write a failing test.
  2. Write the simplest code that passes.
  3. Refactor while keeping tests green.
Code example
Bad example
Python
class PriceCalculator:  def total(self, items):      total = 0      for item in items:          total += item["price"]      return total * 1.21# Manual check after codingprint(PriceCalculator().total([{"price": 100}]))
Good example
Python
def test_total_adds_tax():  calculator = PriceCalculator()  assert calculator.total([{"price": 100}]) == 121class PriceCalculator:  def total(self, items):      subtotal = sum(item["price"] for item in items)      return subtotal * 1.21

Common mistakes

  • Writing tests after the design is already fixed and calling it TDD.
  • Testing too much through mocks.
  • Treating TDD as mandatory for every line of code.