I'm slowing learning that testing is good.
Lets face it, we all know that we should be doing unit tests for everything. But in reality, we don't. Retro-fitting tests onto existing code is pretty slow and painful, so best to avoid it, right? Monday, I had to whip out a quick-fix for something. No problem, I spent all day putting it together, and had it ready to go. That afternoon, I deployed, and stepped though a bunch of data making sure it all ran good, which it seemed to. Because I was in a hurry, I didn't write any tests, I just did all my testing with data I had queued up, and sent through our test environment. On the drive home, I was thinking "Rahn, you should go write some tests for all that stuff you did today, just to see what happens." Tuesday morning, I came in and first thing I saw was an email about a situation that should should have been fixed with yesterday's update. No problem, it's early enough in the day that I could shut down production again for an hour, and get a fix in. Instead of trying to figure out what the problem was by stepping through it with data, I wrote a few unit tests instead. I tracked down the bad data from production, made that into a specific test case, and let it go. And what do you know? I found the bug about 1 second after the test started. Wow, that was cool. 30 seconds later, bug was fixed, test passed, and deployment happening and production is good again. Tracking this bug manually would have taken a good 1/2 hour to step through the program with different data multiple times. The best part: I know have a test framework for this particular program that I can continue. I'll admit, my timing above wasn't completely accurate, since I spent an hour or so building up a couple functions I'll use in the future.
As part of the Monday fix, I did a bunch of refactoring...this program has been on the "clean my code" list for a long time, and so I did it. As I continue to clean it up, I can now test it. Oh, if you're wondering, I'm using Visual Studio 2010 and it's built in test projects. If anyone cares about more details, don't be scared to ask...I need to figure out how to post code blocks on this blog one of these days.
Written on December 26, 2010