real world programming

code coverage

Posted by kevinup on November 4, 2007

I was recently talking to another developer who was going through the process of setting up cruise control. They already had some tests, but wasn’t sure about this thing called ‘code coverage‘, what was it? And how do you use it.

Code coverage, watered down, is a metric to measure what lines of code your tests are covering. On my current project we user ncover. Should you use coverage to evaluate the quality of your tests? Absolutely. Its so easy to hook up to to an existing unit testing framework, there really isn’t an excuse not to be using it.

Next question: What is a desirable coverage? 100% coverage would be awesome, but it extremely difficult to test every single line of code. My current project, our goal is 90% coverage. There are some more rules of thumb I like to follow. Every function should have at least one test on it, and you should only be done testing a function when your functions coverage is above 90%. Also if you are at 90% coverage and you have a 4o lines of untested code, then you need to write more tests.

When evaluating coverage, I’ve been looking at a metric I call ‘Average hits per line’, lets say I have a 20 line function that is hitting 17 lines of code. Before I decide to try and go after another 3 lines of code, I’ll look at the ‘Average hits per line’. It its above something reasonable, like say 4 average hits per line, then you probably could spending your time better working on something else.

To close I’ll talk about my current project. Sadly with my above lecturing, this is a pie in the sky on my current project, in my defense we’ve had probably 20 developers role through our project who had their own agenda’s and ideas on testing, and coverage. My current project has 3 main tiers, 31334 lines of code, and 2393 unit tests. Our total coverage is 85%, 2 of our 3 tiers meet the 90% coverage goal. DataAccess = 97%, ServiceAgents = 92%, BuisnessLogic 78%.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: