Seeing the Test Fail for the Right Reason
You'll learn why watching your test fail initially is a critical validation step in TDD.
The Core Idea
When you write a test first (the "Red" step), you must actually run it and watch it fail before writing implementation code. But here's the crucial part: you need to verify the test fails for the right reason—because the functionality doesn't exist yet, not because of a typo, wrong assertion, or testing framework error.
Why This Matters
Think of it like checking that a smoke detector works by pressing the test button. If you never verify the alarm sounds, how do you know it'll actually alert you to real smoke? Similarly, if you never see your test fail for the expected reason, you can't be confident it's actually testing what you think it is.
A test that never fails might be:
- Testing something that's already implemented
- Written incorrectly and passing vacuously
- Not actually exercising the code path you intended
What "Right Reason" Means
The failure message should clearly indicate the absence of the feature you're about to build. For example:
- Right reason: "Method
calculateDiscountnot found" or "Expected 10, got 0" - Wrong reason: "Syntax error" or "Test file not found" or "Variable undefined"
The first tells you the test is properly structured and waiting for real functionality. The second tells you the test itself has problems.
The Practice
Run your new test immediately. Read the failure message carefully. Ask yourself: "Does this failure confirm I'm testing the right thing?" Only then proceed to write the minimal code to make it pass.
Key Takeaway: Always run your test and confirm it fails with a meaningful error message that validates you're testing the intended behavior—this proves your test has teeth.