Introduction
Writing software can be tricky because bugs often appear after coding. Test-driven development helps prevent bugs by making tests before writing the actual code.
Imagine building a puzzle where you first decide the shape of the next piece (test), then cut the piece to fit (code), and finally smooth the edges to make it look nice (refactor). You repeat this for every piece until the puzzle is complete.
┌───────────┐ fail ┌───────────┐ pass ┌────────────┐
│ Write Test│────────▶│ Write Code│────────▶│ Refactor │
│ (Red) │ │ (Green) │ │ Code │
└───────────┘ └───────────┘ └────────────┘
▲ │
│ │
└─────────────────────────────────────────┘import unittest def add(a, b): return a + b class TestAddFunction(unittest.TestCase): def test_add_positive(self): self.assertEqual(add(2, 3), 5) def test_add_negative(self): self.assertEqual(add(-1, -1), -2) if __name__ == '__main__': unittest.main()