Understanding Code Coverage in Visual Studio Premium 2013

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 4076 red flag
Rating: 5 out of 5  
 2 vote(s)

In this article, we will look into various aspects of Code Coverage in Visual Studio Premium 2013 with a case study.


 Download source code for Understanding Code Coverage in Visual Studio Premium 2013

Recommendation
Read Extract Text from Image using Tesseract in C# before this article.

Introduction

Code coverage is an important asset for a project development.It brings stability in the function of the code block under measure and make the code block stable.The more stability in the blocks of code, the more stability will come in the modules/use cases and henceforth a well balanced unit test cases can be written with less bugs and finally a stable version of the software/modules can be released in the production with least Mean Time To Failure(MTTF) which on the other hand ensures better project/product quality.In this article, we will look into various aspects of Code Coverage in Visual Studio Premium 2013 with a case study.

N.B.~ The Analyze Code Coverage is avaliable under Visual Studio Ultimate and Visual Studio Premium

What is Code Coverage?

It is a verification metric of determining how many lines of code in a given binary are measured when we run test cases against it. By analyzing the result of the code coverage of the Test Methods, we can figure out how much code has been tested for the specified test method. It also provides valuable information to the developer(s) as if the code block is partially, completely or not at all tested which on the other hand helps them to include more test cases to solidify the various boundaries under measure.By knowing how many lines of code of a certain code block are touched, we can judge how well the code/function is tested.

It also provides information about which lines of code have been executed and how long it took to execute them.

What not is Code Coverage for?

  1. It never tells us if the code is well written.
  2. It is not a replacement for through testing since it does not cover all the use cases. Rather, it is a metric for functional testing.

Case Study: Check if a number is positive or negative

Fire up Visual Studio and create a Class Library project. Name it as UtilityLibrary and add a class by the name Utilities.cs and add the below code to it.

namespace UtilitiesLibraryProject
{
    public class Utilities
    {
        /// <summary>
        /// Function: CheckPositiveNegativeNumber
        /// Purpose: Check if a number is positive or negative. If positive return 1 else 0
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public int CheckPositiveNegativeNumber(int number)
        {
            return number > 0 ? 1 : 0;
        }
    }
}

Next open a Console Application, add a reference to UtilityLibrary.dll to it and add the below piece of code

using System;
using UtilitiesLibraryProject;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Utilities objUtilities = new Utilities();
            int n = 5;
            if (objUtilities.CheckPositiveNegativeNumber(n) == 1)
                Console.WriteLine("Positive Number");
            else
                Console.WriteLine("Negative Number");

            Console.ReadKey();

        }
    }    
}

If we run the application, the output will be "Positive Number".

Next, let us add a Unit Test Project

Add a reference to UtilityLibrary.dll to it and write the below piece of code

using Microsoft.VisualStudio.TestTools.UnitTesting;
using UtilitiesLibraryProject;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestForNegativity()
        {
            Utilities objUtilities = new Utilities();
            int inputValue = 0;
            int expected = 0;
            int actual;
            actual = objUtilities.CheckPositiveNegativeNumber(inputValue);
            Assert.AreEqual(expected, actual);
        }
        
    }
}

In the Test Method TestForNegativity, we are passing the value as "0" as input value to the CheckPositiveNegativeNumber function and we are checking the actual value with the expected value which we have already set to "0". Now, run the test case either by pressing Ctrl +R,T or by right clicking on the TestForNegativity Test Method as under

So our test case passed.Now we can start analyzing the Code Coverage either from Test > Analyze Code Coverage > Selected Tests/ All Tests

OR from Test Explorer

After the successful execution of the test(s), Visual Studio calculates the code coverage and displays it in "Code Coverage Results" window

So from the above diagram we can figure out how much code has been covered and it shows that 1 Bolck has not been covered. Hence the % block covered is not 100%. Now in-order to figure out what portions of the code block has been covered fully, partially or not at all, we can use the Show Code Coverage Coloring option of Code Coverage Results window as shown under

So let's click on that and we will get the below

Let us understand the various coloring's

  1. Light blue - Covered
  2. Light orange - Partially Covered
  3. Light red - Not covered

But it is very difficult to understand from the piece of code being written as what has been covered and what not. So let us change our program as below

public int CheckPositiveNegativeNumber(int number)
{
   if (number > 1)
       return 1;
   else
       return 0;
}

Now let us re-run the Analyze Code Coverage for Selected Tests from Test Explorer and the Code Coverage Results is as under

Now let's click on the Show Code Coverage Coloring option and the result is as under

So it is clearly visible that we need to write unit test case for +ve numbers. So let's go ahead and add the below test case

[TestMethod]
public void TestForPositive()
{
    Utilities objUtilities = new Utilities();
    int inputValue = 4;
    int expected = 1;
    int actual;
    actual = objUtilities.CheckPositiveNegativeNumber(inputValue);
    Assert.AreEqual(expected, actual);
}

and run the both the test methods.The result is as under

Now select both the Tests and perform Analyze Code Coverage for Selected Tests as under

The result is as under

Advantages

  1. Bring effectiveness in the code under measure.
  2. Helps to identify unreachable code.
  3. Helps to write better unit test cases.
  4. Helps to measure boundary value analysis of the code block.
  5. Helps to investigate gaps in testing.
  6. Helps to identify redundant tests cases as well.

Reference

Code Coverage

Conclusion

Hope this will be helpful for writing better unit test cases and will bring more stability in the final product/project once it reaches to production environment. Thanks for reading. Zipped file attached

N.B.~ The program presented here is neither to make a comparison between Ternary Operator and If..Else construct nor to show present a bad/good way of writing a code (as mentioned earlier Code Coverage never tells us if the code is well written)

Recommendation
Read Ascii Sort on a Collection in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 1/8/2016 | Points: 25
#Mast article Rajnilari2015. A lot to learn from this.

Keep it up!

Thanks
Posted by: Rajnilari2015 on: 1/8/2016 | Points: 25
@Sheonarayan Sir, thanks a lot...
Posted by: Rajeshatkiit on: 1/9/2016 | Points: 25
Good article.
Posted by: Rajnilari2015 on: 1/9/2016 | Points: 25
@Rajeshatkiit Sir, thanks.

Login to post response

Comment using Facebook(Author doesn't get notification)