Bill Blogs in C#

Bill Wagner discusses C#, LINQ, and other items of interest

I hate testing

And that's why I write automated tests.

I recently inherited some truly bad code. I'd post some examples, but the codebase is owned by a client, and the code is covered by an NDA. But, to give you an idea how bad it is, here are a few interesting statistics from when I first examined the code:

Longest method: 1779 lines (that's not a typo. The same method has a cyclomatic complexity of 204).
Number of public fields: 527
And, most significantly: Number of unit tests: 0.

This project had a bad code smell that rivaled an unchecked diaper pail. It reeked.

So, I set about making changes.

As I said above there were no automated tests. So I'm still doing a reasonable set of hand testing with each change. That slows me down. But, my first step as I refactor each class is to create a series of unit tests that help me understand what the code does. As I refactor, I clean up the interface. Some tests go away (because some methods go away). The body of some tests are moved around. (While methods are removed, the functionality is needed, so it moves into other methods. The same test logic exercises those new methods).

It's a slow process, but it does show in the quality of the code. I've now got more than 250 unit tests. I can press one button and a few minutes later, a reasonable subset of the code is tested. My unit tests currently hit 94% of the code I've refactored. I'd like to keep it that high as I continue refactoring. I still need to exercise too much by hand, because I'm not done refactoring. (I still haven't tackled the class with the 1700 line method).

The point of this little rant is that hand testing is boring. It's also error prone. And, it's hard to tell if you've touched everything. It's also time consuming. And, it's a recurring time sink. When I write automated tests, I pay a one time cost, and I get to reuse that work to validate the code quality with every change.

That's one of the biggest gains from automated tests: you trade a recurring cost (hand testing) for a one time cost (automated testing). Yes, there are many other benefits: Code quality, isolating errors, writing testable code, etc. But yet, the one silver bullet for management is that you trade this recurring cost (a constant drain on productivity) for a one time cost (writing tests).


 

Published Thursday, October 18, 2007 10:43 PM by wwagner
Filed under: ,

Comments

# re: I hate testing@ Monday, October 22, 2007 4:31 PM

Bill - What tools do you use most often for automated tests?  What are their pros and cons?  Why would you use one over another?

Regards,

Scott H.

by Scott H.

# re: I hate testing@ Sunday, October 28, 2007 9:35 AM

At the moment, I'm using the built in unit test functionality in Visual Studio.  In the past, I've used NUnit. Both of them are capable of anything you need, they just do things differently.

Historically, I've had problems with NUnit when the assembly I'm testing includes unmanaged code. NUnit (at the moment) does a better job of leading you through tests that require user interaction.  I still use both, although I am trending toward the VS version.

by wwagner