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
    public class UnitTestCaseDemo
        public void AdditionOfTwoNum()
            var a = 0;
            var b = 0;
            if (a!=0 && b!=0)
                var result = a + b;
                Assert.AreEqual(30, result);
                var result = a + b;
                Assert.AreEqual(0, result);

        public void SubstractionOfTwoNum()
            var a = 20;
            var b = 10;
            var result = a - b;
            Assert.AreEqual(10, result);

        public void MultiplicationOfTwoNum()
            var a = 20;
            var b = 10;
            var result = a * b;
            Assert.AreEqual(200, result);

        public void DivisionOfTwo_Num()
            var a = 20;
            var b = 10;
            var result = a / b;
            Assert.AreEqual(2, result);

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

        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

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

Code Coverage Tool (Part 2)


Before reading this post, please go through my previous article what is the code coverage tool

In this post we will see one of famous tool i.e. DotCover Code Coverage

It is the JetBrains Code coverage tools.It is paid version tool. It has the following features
1. Continuous unit testing
2. Code highlighter
3. Hot spot feature for breaking code.
4. Save code coverage report in HTML,XML and Json format

For download the trail version of this tool, you ca download from this URL

Before using this tool you have to also download and install re-sharper Tool.

If you have installed the tool successfully then you will get the resharper Menu in Visual Studio and from there you can select the Cover unit test case. You can also right click solution explorer and select the Cover Unit Tests and click on it like given below image

It will show the result like this

In the above image, it is showing which method has how much the code coverage.

It has the different type of save report options like HTML,Json and XML format

Example of HTML format Report

Example of JSON format Report

Example of XML format Report

Example of Json format Report

using the above report we can easily find out the uncovered test case in our application.

What is the code coverage tool ? (Part1)

Code coverage is the process of doing analysis in C# unit test code to find out which block or statement of code is executed or not.

The basic code coverage criteria are

1. Function coverage
2. Statement coverage
3. Branch Coverage
4.Conditional Coverage

There are so many tools available for this task

1. Visual Studio code coverage (Only in enterprise edition)
2. NCover
3. JetBrains dotCover
4. OpenCover
5. NCrunch
6. NDepend

I had gone through the few important code coverage tool

1.Visual Studio code coverage (Only in enterprise edition)

>> In visual studio enterprise edition default code coverage tool will be there. Since i am not using the enterprise version, I did not do deep on analysis on this tool. If you are using the enterprise edition you can go through the below URL

2. NCover Code Coverage
>> Ncover is third party tool for visual studio.It is not the free tool. For this we have to download the software from given below URL

This tool is very straight forward to use. Firstly we have to install the tool then we will get the Ncover Menu in visual Studio.
Click on Ncover Code Coverage then you will get the Ncover Test Runner. It will load all the test case written in your application

Here i have written the test code like this

In the above test method, it will run only else condition so we are not testing the code of if condition. So if we will run the NCover tool it will show covered code as green and not covered code as red as given in above image.

It helps to find out the uncovered code in test application. This tool has also option to view the result on Desktop. It has also option to download the report in different format like html,Json and XML.

Posted in Tools. 1 Comment »