Fundamentals of Unit Testing: Code Coverage Using Visual Studio Unit Testing

This is the “Fundamentals of Unit Testing” article series. In our previous article we learned various concepts of unit testing. You can read them here:

The concept of code coverage may be new to many people but the concept is very simple. Code coverage is one parameter to measure the quality of software. The concept of code coverage is like this, it will tell you what portion of your code is covered by a unit test. Good software must cover at least 70-80 percent of code. And it's always recommended to cover at least 60% of code during application development.

Fortunately to measure code coverage, we need to use any third-party tool, Visual Studio unit test application is enough to measure code coverage. So, let's create one class library application and write the following function in it, we will test this function using a unit test method and we will measure the code coverage of unit testing. Have a look at the following code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace TestProjectLibrary

{

    public class calculator

    {

        public int Add(int a,int b)

        {

            return a + b;

        }

        public int Sub(int a, int b)

        {

            return a - b;

        }

        public int Mul(int a, int b)

        {

            return a * b;

        }

        public int Div(int a,int b)

        {

            return a / b;

        }

    }

}

Four functions are very simple; it will just perform operations of a simple calculator. Now, add one unit test project in same solution and provide a reference of this application to the unit test application. If you don't know how to configure a unit test then I that request you go through our first article where you learn to add a unit test project to the solution. Here is the code for unit testing.

using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using TestProjectLibrary;

 

namespace UnitTest

{

    [TestClass]

    public class UnitTest

    {

        [TestMethod]

        public void TestMethod1()

        {

            calculator obj = new calculator();

            Assert.AreEqual(20, obj.Add(10, 10));

            Assert.AreEqual(10, obj.Sub(20, 10));

            Assert.AreEqual(100, obj.Mul(10, 10));

            Assert.AreEqual(10, obj.Div(100, 10));

        }

    }

}

Please have a look that we are testing all four methods here and if we run a test, all tests should pass. So, let's run the test.

run the test

And we are seeing that the test has passed, we will now check the code coverage in this unit test. If you are using Visual Studio 1012 then go to the following tab to run the code coverage.
 
code coverage

Once the test is successfully executed, we will find a code coverage window like below.
 
code coverage window

And we are seeing that the entire code has been covered by our unit test. Let's block one test and perform code coverage again, have a look at the following example.
 
unit test

We have clocked the checking of the div() function and it's showing that we are covering only 87.5% of code, not 100 %.

Conclusion

It's always a best practice to check your code coverage during unit testing to confirm that your code will not crash in any scenario. Happy reading.

Up Next
    Ebook Download
    View all
    Learn
    View all