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
- Write a failing test.
- Write the simplest code that passes.
- Refactor while keeping tests green.
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}]))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.21Common 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.