Code Coverage In xUnit.net Unit Test Projects In Visual Studio Enterprise 2017

In this tutorial, we will understand the code coverage concepts in Visual Studio 2017 Enterprise and how to include and exclude the necessary part of the code.

In this tutorial, we will understand the code coverage concepts in Visual Studio 2017 Enterprise and will learn how to include and exclude necessary parts of the code step by step from scratch in Visual Studio 2017 Enterprise.

Prerequisite
Step 1

Create a library project ("TDD.xUnit.net.Client") and setup xUnit.net unit test project. If you are not aware of setting up Xunit unit test project, then refer to the article - Setup xUnit.net Unit Testing In Class Library Project.

Step 2

Create a library project ("Calculator.Lib") and a test project ("TDD.xUnit.net.Client") as in the following screen.

Code Coverage in xUnit.net Unit Tests Projects in Visual Studio Enterprise 2017

Step 3

Open "Calculator.cs" file from "Calculator.Lib" project and add the following lines of code.
  1. namespace CalculatorLib  
  2. {  
  3.     public class Calculator  
  4.     {  
  5.         public int Add(int x, int y)  
  6.         {  
  7.             if (x == 0 && y == 0)  
  8.                 return 0;  
  9.             else  
  10.                 return x + y;  
  11.         }  
  12.     }  
  13. }  

Step 4

Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and add following lines of code.

  1. using CalculatorLib;  
  2. using System.Diagnostics.CodeAnalysis;  
  3. using Xunit;  
  4.   
  5. namespace TDD.xUnit.net.Client  
  6. {  
  7.     [ExcludeFromCodeCoverage]  
  8.     public class CalculatorTests  
  9.     {  
  10.         [Fact]  
  11.         public void PassingTest()  
  12.         {  
  13.             var calculator = new Calculator();  
  14.             Assert.Equal(4, calculator.Add(2, 2));  
  15.             Assert.Equal(0, calculator.Add(0, 0));  
  16.         }  
  17.     }  
  18. }  
Step 5

Now, build your solution and open the code coverage window from VS 2017 Enterprise from menu Tests=>Analyze Code Coverage=>All Tests as in the following screen.

Code Coverage in xUnit.net Unit Tests Projects in Visual Studio Enterprise 2017

Once you click on "All Tests", you will see the "Code Coverage Result" window as in the following screen.

Code Coverage in xUnit.net Unit Tests Projects in Visual Studio Enterprise 2017 

Here, one question arises, which is, why are we getting 100% code coverage? We have an extra line of code, except  the "Add" method of "Calculator" class because we excluded all lines of code of the "CalculatorTests" class with the help of the attribute "ExcludeFromCodeCoverage," and we are testing all possible scenarios of the Add method of calculator class; i.e., passing zero and non zero values to add method.

Step 6

Now, let's do one more experiment and comment zero assertion in xUnit unit tests as in  the following screen.

Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and add the following line of code.
  1. using CalculatorLib;  
  2. using System.Diagnostics.CodeAnalysis;  
  3. using Xunit;  
  4.   
  5. namespace TDD.xUnit.net.Client  
  6. {  
  7.     [ExcludeFromCodeCoverage]  
  8.     public class CalculatorTests  
  9.     {  
  10.         [Fact]  
  11.         public void PassingTest()  
  12.         {  
  13.             var calculator = new Calculator();  
  14.             Assert.Equal(4, calculator.Add(2, 2));  
  15.             //Assert.Equal(0, calculator.Add(0, 0));  
  16.         }  
  17.     }  
  18. }   
Code Coverage in xUnit.net Unit Tests Projects in Visual Studio Enterprise 2017

Now, you can see that the code coverage has been reduced and is 71.43 % because we have commented the zero assertion and are not touching zero value logic in the add method in the calculator class from the above screen. Now, we are not covering 28.57% of the code; i.e., only 71.43% of the code is being unit tested and 28.57% of the code is not being unit tested.

If you write more code and do not write a unit test then code coverage will be decreased; if you write a unit test then code coverage will be increased.

If there is any difficult-to-test code, for example, network, database, unit test, class, or method etc. just use attribute
"[ExcludeFromCodeCoverage]" either class or method level. I hope now it is clear what code coverage is and how to control it.

Step 7 - Done.

Congratulations! You have successfully learned what  code coverage is and how to control the code coverage in Visual Studio 2017 Enterprise. If you have any query or concern, just let me know or just put in the comment box and I will respond as soon as possible. I am open to discussing anything, even silly questions as well. If you have any suggestions related to this article, please let me know and I promise I will improve this article to a maximum level.

Summary

In this article, we have learned what code coverage is and how to control it  in Visual Studio 2017 Enterprise in C#.