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?

Latest Posts

Popular Posts

 
Google Analytics Alternative