๐Ÿ† Testing & TDD

The capstone: prove your code works

โ† Back to Architecture

๐Ÿ›ก๏ธ Why test?

A test is code that checks your other code. Instead of running your program and eyeballing the output, you write tests that automatically prove it works โ€” and keep proving it as you make changes.

Tests are a safety net. When you add a feature and a test suddenly fails, you've caught a bug before your users do.

โœ… The assert statement

The simplest test tool: assert checks that something is true. If it isn't, your program stops and tells you.

def add(a, b): return a + b assert add(2, 3) == 5 # passes silently assert add(0, 0) == 0 # passes print("All tests passed!")

๐Ÿ’ก Read it as a promise

assert add(2, 3) == 5 means "I promise add(2, 3) equals 5." If the promise breaks, Python raises an error.

๐Ÿ”ด๐ŸŸข Test-Driven Development (TDD)

TDD flips the usual order: you write the test first, watch it fail, then write just enough code to pass.

StepColorWhat you do
1. Red๐Ÿ”ดWrite a failing test for a feature you want.
2. Green๐ŸŸขWrite the simplest code to make it pass.
3. Refactor๐Ÿ”ตClean up the code โ€” tests keep you safe.

๐Ÿงช Test the edges

Good tests check normal cases AND tricky edge cases: empty lists, zero, negatives, and the very first/last item.

def biggest(nums): return max(nums) assert biggest([3, 7, 2]) == 7 # normal assert biggest([5]) == 5 # edge: one item assert biggest([-1, -9]) == -1 # edge: negatives

๐ŸŽ“ You finished the track!

  • Clean Code โ€” readable names and small functions.
  • OOP โ€” classes, objects, inheritance.
  • SOLID โ€” five principles for flexible design.
  • Patterns โ€” factory, strategy, observer.
  • Modules โ€” separation of concerns.
  • Testing & TDD โ€” prove it works, automatically.

๐ŸŽ‰ You now design software like a professional engineer. Incredible work!