Stephen A. Fuqua (SAF) is a Bahá'í, software developer, and conservation and interfaith advocate in the DFW area of Texas.

TDD - Scenario for Red, Green, Refactor

September 3, 2009

Here's a really dumb scenario that will illustrate a point about the usefulness of the "red, green, refactor" approach to testing and coding. Here's the functionality - need to test whether or not a string has a value other than 1. Let's say I write a method before any tests:

public static bool MyMethod(string value)
    return value.Equals("1");

And let's say I write my test:

public void t_MyMethod_NotOne()
    bool returnValue = false;

The test will always pass! I wrote it incorrectly, I forgot to set returnValue = MyMethod("2"). If instead I had written this test with a "red, green, refactor" mindset, then I would have immediately seen that the test was green when it should be red – and then I would go back and realize that I had written the test incorrectly.

Good related links:

  1. Red-Green-Refactor (random blogger)
  2. Guidelines for Test-Driven Development (Microsoft)
  3. Unit Test Rulz (random forum post)