Test-Driven Development (TDD) Workflow
What TDD is
Section titled “What TDD is”TDD is a workflow:
- write a failing test (Red)
- write minimal code to pass (Green)
- refactor safely (Refactor)
Diagram: red-green-refactor
Section titled “Diagram: red-green-refactor”graph LR A[Red\nFailing test] --> B[Green\nMake it pass] B --> C[Refactor\nImprove design] C --> A
Why TDD helps
Section titled “Why TDD helps”- forces clarity on requirements
- encourages smaller functions
- reduces fear of refactoring
Realistic TDD
Section titled “Realistic TDD”TDD is easiest for:
- pure functions
- business rules
Harder for:
- UI code
- code with many external integrations
In those cases, combine TDD with integration tests.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Write a unittest TestCase
Section titled “Exercise 1 – Write a unittest TestCase”Exercise 2 – assertRaises
Section titled “Exercise 2 – assertRaises”Exercise 3 – setUp and tearDown
Section titled “Exercise 3 – setUp and tearDown”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading