The Fake It Pattern
You'll learn how to get tests passing instantly by returning hardcoded values, then gradually refactoring to real logic.
What is the Fake It Pattern?
When practicing Test-Driven Development, the Fake It pattern means making your test pass in the simplest, even embarrassingly simple way: return a hardcoded value that satisfies the test. Once the test is green, you refactor incrementally toward the real implementation, guided by additional tests.
This might feel like cheating—and that's the point! By "faking" the implementation first, you maintain the Red-Green-Refactor rhythm without getting bogged down in complex logic prematurely.
How It Works
Imagine writing a test for a function that adds two numbers:
def test_addition():
result = add(2, 3)
assert result == 5
Instead of writing the full addition logic immediately, you fake it:
def add(a, b):
return 5 # Hardcoded!
The test passes! Now you're in the Green phase. When you write a second test with different numbers, the hardcoded 5 fails. That failure drives you to generalize:
def add(a, b):
return a + b # Real implementation emerges
Why Fake It?
Think of it like building with training wheels. The hardcoded value keeps you moving forward, prevents overthinking, and ensures you're always one small step from working code. It forces you to add more tests (triangulation) that reveal what the real implementation must do.
It's particularly useful when you're stuck or the solution isn't obvious—faking it gets you unstuck and maintains momentum.
Key Takeaway: Start with the simplest thing that could possibly work—even a hardcoded return value—then let additional tests drive you toward the real implementation incrementally.