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:
[Test] public void t_MyMethod_NotOne() { bool returnValue = false; MyMethod("2"); Assert.IsFalse(returnValue); }
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:
- Red-Green-Refactor (random blogger)
- Guidelines for Test-Driven Development (Microsoft)
- Unit Test Rulz (random forum post)