Search This Blog

Thursday, April 29, 2010

Finding Root Cause More Quickly with Unit Tests

I can't emphasize enough the importance of writing good meaningful unit tests. This was reinforced again today when I received a request from one of our folks who said there was a bug with one of the systems for which I'm responsible. My immediate reaction was that I didn't trust the code I had written months ago. The scenario is pretty simple. If one property of a class is null, output a default value. The code is:

Writer.Write(instance.AdText ?? "Click link for more info....");

Silly me. I didn't trust the .NET Framework to work as expected so I wrote a unit test to verify that when AdText is null, the default is printed.

The bad news is I wrote the test. The good news is, it disproved my immediate invalid assumption of where to start chasing the bug down. Months ago, the process would have been to build and deploy the application then run it with an email subscriber and ads that have and don't have AdText properties. Alas, the same conclusion would have been reached but with the unit test, my silliness was pointed out to me far more quickly.