Course contentsShow
← Software Engineering
Lesson 499 of 1,93315. Test-Driven DevelopmentFree lesson

Triangulation: Adding Test Cases

Add multiple test cases with different inputs to force generalization of hardcoded or overly-specific solutions.

Triangulation: Adding Test Cases

You'll learn how adding multiple test cases with different inputs forces you to replace hardcoded values with real, generalized logic.

The Core Idea

After using the "Fake It" pattern to get your first test passing with a hardcoded value, you're not done. You need to triangulate by adding more test cases with different inputs. When you have multiple examples that all need to pass, you can no longer fake it—you must write the actual implementation.

Think of it like finding your location with GPS. One satellite signal isn't enough; you need multiple signals from different positions to pinpoint where you are. Similarly, one test case doesn't prove your code works generally—you need multiple test cases from different "angles" to force the real solution.

How It Works

Let's say your first test checks that add(2, 3) returns 5. You could make it pass by returning 5 directly (the Fake It pattern). That's fine for the first step.

Now add a second test: add(1, 4) should also return 5. Suddenly, your hardcoded 5 still works! You need a third test with a different expected result: add(2, 2) should return 4.

Now you're forced to generalize. You can't hardcode a single number anymore. You must write return a + b to satisfy all three test cases. The multiple test cases triangulated you toward the correct implementation.

When to Use It

Use triangulation when you find yourself writing obvious fakes or when you're unsure what the general algorithm should look like. Each new test case reveals more about the pattern your code must follow.

Key Takeaway: Multiple test cases with varied inputs and outputs force you to abandon hardcoded solutions and implement genuine, generalized logic—triangulation drives you from fake implementations to real ones.