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 2
Create a library project ("Calculator.Lib") and a test project ("TDD.xUnit.net.Client") as in the following screen.
Step 3
Open "Calculator.cs" file from "Calculator.Lib" project and add the following lines of code.
- namespace CalculatorLib
- {
- public class Calculator
- {
- public int Add(int x, int y)
- {
- if (x == 0 && y == 0)
- return 0;
- else
- return x + y;
- }
- }
- }
Step 4
Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and add following lines of code.
- using CalculatorLib;
- using System.Diagnostics.CodeAnalysis;
- using Xunit;
-
- namespace TDD.xUnit.net.Client
- {
- [ExcludeFromCodeCoverage]
- public class CalculatorTests
- {
- [Fact]
- public void PassingTest()
- {
- var calculator = new Calculator();
- Assert.Equal(4, calculator.Add(2, 2));
- Assert.Equal(0, calculator.Add(0, 0));
- }
- }
- }
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.
Once you click on "All Tests", you will see the "Code Coverage Result" window as in the following screen.
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.
- using CalculatorLib;
- using System.Diagnostics.CodeAnalysis;
- using Xunit;
-
- namespace TDD.xUnit.net.Client
- {
- [ExcludeFromCodeCoverage]
- public class CalculatorTests
- {
- [Fact]
- public void PassingTest()
- {
- var calculator = new Calculator();
- Assert.Equal(4, calculator.Add(2, 2));
-
- }
- }
- }
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#.