How to increase the Code Coverage in 2 easy steps

As a developer you come to the point in time where a manager asks you to have at least 80% Code Coverage in your code.

Manager 
Photo Credit: Sam Foster

All managers love to see numbers, especially big increasing numbers.
I love big numbers too!

The nasty ones of you might ask a question back:

  • “Why do you want those numbers?” or
  • “What do you need those numbers for?” or
  • “Who are you going to show them?”

But let’s not do a “Root Cause Analysis” here and just solve the symptom.

Lets look at this project and the Code Coverage for it.

imageFigure: Before: 74.83 % Code Coverage

 

Here is how you can increase your code coverage in 2 easy steps.

Copy the following into your production code

// Call this method something important.
// Use words like: Manager, Proxy, Controller, Command, Dispatcher, Broker, Container …
public
bool IsMessageProxyControllerManager()

{ //Repeat more often to increase Code Coverage CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); CreateNewImplementation(); return true; }
// Rename this to something important.
// Mix 2 words from: Invoke, Caller, Fetch, Request, Factory, Synchronizer, Shepherd, Coordinator
//
// If you cant come up with something use https://github.com/ab9/classnamer
public void CreateNewImplementation() { }


Copy the following in your test code

[TestMethod]
public void GivenANewInstance_MessageProxyControllerManager_ExpectAPositiveAnswer()
{
    var c = new ClassWhereYouCopiedThe2Methods();  // Change this
    var result = c.IsMessageProxyControllerManager();
    Assert.IsTrue(result);
}

 

Run the Code Coverage Tester again, and Voila!

image
Figure: After: Increased Code Coverage from 74% to 79% Code Coverage

 

Think about what Code Coverage really tells you

  • Does it really measure code lines tested?
  • Or does it measure code lines executed?
  • What does that number tell you?
  • Is it of value to you? To a manager?
  • Is 80% Code Coverage a good goal?

 

Now… what are you going to tell your manager?

4 comments:

The Sage said...

This, of course, is why you have code reviews, so that tricks like this don't make it into source control in the first place.

And if all your devs would collude to let it past, then that's likely to be the least of your problems.

Nick said...

Lol I'm pretty sure that is not the point.

Anonymous said...

This is cheating. You cheating your managers - sure no problem. You cheating your colleague as well. Do you really want to do this ?

Peter Gfader said...

>>You cheating your colleague as well
Not really since they know about this..


>>You cheating your managers - sure no problem
If those are happy -> job done. Or is there a higher goal than making your managers happy?

Post a Comment

Latest Posts

Popular Posts