Wednesday, May 30, 2007

Code Coverage Analysis

Late last week a coworker asked me whether it was possible to see code coverage information for tests. Of course! I said, so since I had a project open with a bunch of unit tests, I right-clicked "Run Tests... With Coverage". Then I just let TestDriven.Net, MbUnit, NCover and NCoverExplorer do their magic. Needless to say we were both impressed! The resulting output showed me that I'd achieved 85% coverage without even trying very hard. The flip side was that I'd missed several interesting cases. Code coverage isn't everything but it is a useful metric for judging test quality. So I proceeded to use code coverage analysis to guide my testing efforts for the next few days. I found a couple of bugs because code coverage data indicated that certain unit tests weren't quite reaching their mark. The unit tests had not failed but the optimizations being tested had been implemented incorrectly in ways that were compensated for elsewhere. Subtle. Beware hidden behavior that cannot be observed! Ahh! So good to reaquire old habits! Now the Castle.Components.Scheduler project has 248 unit tests with 99% code coverage. Pretty neat!

No comments: