Welcome to MSDN Blogs Sign in | Join | Help

James Newkirk's Blog

Adding to the kipple... One post at a time.
How to make sense of Code Coverage metrics

(co-authored with Brad Wilson)

Even since the release of Visual Studio Team System 2005 with easy to use
code coverage tools, people inside (and outside) of Microsoft have been talking about what it means. The most common approach we've seen is for a team leader to mandate a minimum code coverage number (like "all code must have at least 80% code coverage before being checked in"). As agile advocates, you'd expect that our team's TDD'd code would have 100% code coverage. And you'd be wrong.

We could all agree that test-covered code is better than not, so we probably also agree that a high coverage number is better than a low one. Why can't we come up with a hard and fast number that means "good"? The answer is that it's different for every project, and even for a single project, that number may change over time. It is inevitable that code is written which isn't covered by a unit test. A few examples of acceptable code without tests might include: web service wrappers generated by Visual Studio, views in a Model-View-Presenter system, and code for which failure is only possible because the underlying platform fails (like helper methods that pass default values into more complex .NET CLR methods).

So when you run that code coverage system for the first time and it pops up "73%", what next? Well, if it's the first time you've run the test, you're probably going to check out that other 27% and see how you categorize it. Does that code need coverage or not? If so, use the lack of code coverage to educate yourself on taking smaller steps in TDD. But if not, what then? Is 73% a magical number?

The answer lies in a metric that can be used to predict the weather,  barometric pressure. Today, weather.com says the barometric pressure in Seattle is 29.66”Hg and falling. More important than the absolute measurement is the trend: it tells you that the overcast and drizzling rain outside is going to get worse, not better.  The two pieces of information - value and trend - are used to predict what will happen next. The same can be said for code coverage. You can determine the relative health of your tests by using the value and the trend to determine the appropriate action.  That’s the important bit: code coverage gives you relative measurements against itself, not an absolute measurement against a target value. If your coverage fell from 73% to 72%, would you be worried? What if it fell from 73% to 67%?

Rather than holding your team to an absolute goal, use the trend to determine when you should invest time to figure out where you're covered and not, and to determine what to do about it.

Posted: Sunday, February 25, 2007 3:14 PM by jamesnewkirk

Comments

Brad Wilson - The .NET Guy said:

# February 25, 2007 6:43 PM

Team System News said:

The UK Developer Tools Team Blog on Virtual PC 2007 and Team Build. Ed Hintz on Channel 9 Interview...

# February 26, 2007 9:04 AM

Espresso Fueled Agile Development said:

Jim Newkirk and Brad Wilson have an interesting blog post on code coverage and what it means . They have

# February 26, 2007 6:52 PM

jrshaw said:

These are good examples of my fundamental rule that metrics must be interpreted within context.  You can read more at http://blog.panopticode.org/articles/2007/02/26/metrics-must-be-interpreted-in-context

# February 27, 2007 1:03 AM

Paulo Morgado said:

That's why I like mock frameworks (http://msmvps.com/blogs/paulomorgado/archive/2007/02/17/unit-testing-and-mock-frameworks.aspx). I can get near 100% code coverage (I want an absolute 100% but Visual Studio says 98% and can't paint any uncovered code). I usually mock my onw internal members to test my public members.

# February 28, 2007 4:04 AM

MattYoung said:

Very true, code coverage only revels what is executed, not what is actually verified, http://flickr.com/photos/niallkennedy/330227455/.

What we really want are unit test analysis tools. Tools which analyze unit tests to determine how effective they are at detecting changes to the code under test. Jester is an example, (http://jester.sourceforge.net/), which makes changes to the code under test and then runs the unit tests to determine if the change is detected.

This area is still fairly green, but certainly has a lot of potential.

# March 2, 2007 2:27 PM

Christian Bøgh Jensen's Blog said:

James Newkirk has an interesting post on code coverage metrics . It's impossible to use a hard code coverage

# March 7, 2007 4:06 AM

Sam Gentile said:

Still real tired from my Oklahoma trip , partying with Raymond sure is exhausting-). Agile/Development

# March 7, 2007 12:10 PM

Brad Wilson - The .NET Guy said:

# May 7, 2007 6:51 PM

Sam Gentile's Blog said:

Still real tired from my Oklahoma trip , partying with Raymond sure is exhausting-). Agile/Development Tools On my short list for some time now, is to switch from NUnit to the definitely superior MbUnit. My friend Andrew has done some great work with

# December 3, 2008 10:09 AM

Termékinformációk fejlesztőknek said:

[ Nacsa Sándor , 2009. január 19. – február 5.] Ez a Team System változat fejlett eszközrendszert kínál

# February 5, 2009 12:44 PM

Termékinformációk fejlesztőknek said:

[ Nacsa Sándor , 2009. február 6.] Ez a Team System változat a webalkalmazások és –szolgáltatások teszteléséhez

# February 6, 2009 6:56 AM
Anonymous comments are disabled
Page view tracker