Code Coverage Tool (Part 3)


This is the last part of code coverage tool, in this post we will see the open source code coverage tool,i.e. OpenCoverage which is light weight and totally free to use. Which will give almost all functionalities as similar to third party code coverage tool (Ncoder and DotCover)

NCover Code Coverage Tool
DotCover Coverage Tool

How to use OpenCover tool?

Step 1: Create some demo unit test case in visual studio. I have written some dummy unit test case as given below

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestCase_Sample
{
    [TestClass]
    public class UnitTestCaseDemo
    {
        [TestMethod]
        public void AdditionOfTwoNum()
        {
            var a = 0;
            var b = 0;
            if (a!=0 && b!=0)
            {
                var result = a + b;
                Assert.AreEqual(30, result);
            }
            else
            {
                var result = a + b;
                Assert.AreEqual(0, result);
            }
        }

        [TestMethod]
        public void SubstractionOfTwoNum()
        {
            var a = 20;
            var b = 10;
            var result = a - b;
            Assert.AreEqual(10, result);
        }

        [TestMethod]
        public void MultiplicationOfTwoNum()
        {
            var a = 20;
            var b = 10;
            var result = a * b;
            Assert.AreEqual(200, result);
        }

        [TestMethod]
        public void DivisionOfTwo_Num()
        {
            var a = 20;
            var b = 10;
            var result = a / b;
            Assert.AreEqual(2, result);
        }

        [TestMethod]
        public void Addition_Of_ThreeNum()
        {
            var a = 10;
            var b = 20;
            var c = 30;
            var result = a + b + c;
            Assert.AreEqual(60, result);
        }

        [TestMethod]
        [ExpectedException(typeof(DivideByZeroException))]
        public void Test()
        {
            int i = 10, j = 0;
            int x;
            x = i / j;
        }
       
    }
}

Step 2: Download the OpenCover UI from Microsoft store, from the given below URL

https://marketplace.visualstudio.com/items?itemName=jamdagni86.OpenCoverUI

In Visual Studio Tab will show like this

Step 3: Right click on solution explorer and click on Manage NuGet Packages and Install the OpenCover and ReportGenerator in your application as given below

Step 4: Go to Visual studio Tool ->> Options ->> OpenCoverUI Options then select the OpenCover.Console.exe from your application.

Step 5: Go to OpenCover tool of Visual Studio and Select OpenCover Test Explorer and select the test cases to be run like given below Image

Now we will get output like given below

In the above result it is showing that how much code has been covered in which test case.

If you have to fix the problem then just double click on given method, you will see the code like this given below image

This indicate that below code branch has not been executed. So we have to write the other unit test case to cover above code.

How to get the report as third party control as similar to Ncover and DotCover

For getting the report in user friendly manner we have to take help of Report Generator Tool.
For that we have to create the .bat file, which will generate the report in given folder.

Firstly create the notepad file keep this syntax as given below

"C:\Source\Test \packages\OpenCover.4.6.519\tools\OpenCover.Console.exe" -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\mstest.exe" -targetargs:"/noisolation /testcontainer:\"C:\Source\Test \Test\bin\Debug\Test.dll\" /resultsfile:C:\Reports\MSTest\.trx" -mergebyhash -output:C:\Reports\MSTest\projectCoverageReport.xml


"C:\Source\Test \packages\ReportGenerator.2.5.2\tools\ReportGenerator.exe" -reports:"C:\Reports\MSTest\projectCoverageReport.xml" -targetdir:"C:\Reports\CodeCoverage"

Now save the file as .bat extension and run the bat file.

Note: Before running the bat file, please create the Reports\CodeCoverage and MSTest directory in C folder.

Now go to the CodeCoverage folder and see the report after clicking on Index.html file like as given below

In the above tools compare we show that OpenCover contains almost all the major features which contains all the paid version tools and it is very light weight and flexible to use in any project.

Comparison between all the Tools

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: