Analyzing code coverage in Visual Studio
Tags: dotnet, csharp, testing
Published: 2020-07-12 22:53:42

In the [previous installment](https://dev.to/rruizdev/cobertura-del-codigo-en-net-herramdamientos-4ga9) we recognized the tools that we can use to validate our code and its coverage. 

Today, we are going to focus on the operation of the tools that Visual Studio provides us.

![Visual Studio](https://dev-to-uploads.s3.amazonaws.com/i/gmi7plbs8vco6oxhtpy1.jpg)

---

# Fundamental

## Visual Studio version

To take advantage of this proposal, you must have Visual Studio in its Enterprise version, which is paid.

## Project with Unit Tests

It is worth clarifying that, to follow these steps, the project must have unit tests. Otherwise, I will tell you briefly as below.

### Create a Unit Test Project

We right-click on the solution, and go to `Add > New Project`.

![](https://dev-to-uploads.s3.amazonaws.com/i/y4pivlpilpkek8oht36j.png)

It can also be _MSTest_ or _NUnit_, which runs at your choice. You may also need to install additional extensions.

![](https://dev-to-uploads.s3.amazonaws.com/i/tnq4f6q11r6warq0i3iq.png)

---

_Just as we create projects for libraries or the API itself, we also have to create one for testing._

---

### Create a Test Class

Following the same structure of the project to be tested, we create a mirror class (the same name, although with the suffix _ "Test"_).

---

_It is essential that you follow a structure that allows you to recognize your test scheme, to guarantee the scalability of your project._

---

![](https://dev-to-uploads.s3.amazonaws.com/i/mc2zi9aby2yzwi38ncar.png)

To do this, we right-click on the folder in question and go to `Add > New item` and within it, we select _"Class"_.

### Creating a Unit Test

Inside the created class, we configure its _setup_ and then, the test itself.

![](https://dev-to-uploads.s3.amazonaws.com/i/j1wwiqmbudnk20sqh7yx.png)

The example is in NUnit, although from there we can understand that it is good to work with the ["AAA" pattern] (https://docs.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2019#write-your-tests) and that the [differences between NUnit and xUnit] (https://xunit.net/docs/comparisons) are minimal.

---

# Ok, let's do it!

## Using the Test Explorer

The _Test Explorer_, as a tool, allows us to run tests of several projects and the _Test Class_ that correspond to it, under various testing frameworks.

Now, to run our tests, we have to land on our unit tests project, and we click `Run Tests`.

![](https://dev-to-uploads.s3.amazonaws.com/i/97x666c4hi9u6in907lo.png)

Whether or not you have compiled the solution, the solution will be pre-compiled anyway.
Then, you will have a new window, called **Test Explorer**, in which the tests will be run.

![](https://dev-to-uploads.s3.amazonaws.com/i/zibb45057zs57kg6a4a2.png)

### To consider

- In the left area, you will see the structure of the test project and the result of each one.
- On the right, you will see the details in case you had a different result than expected.
- At the top, shortcuts to quick functionalities and filters according to the result of each test.

## Analyzing Coverage

Since we ran the tests under the _Test Explorer_, now we can get excited with this tool, which seeks to give us feedback on the coverage of our code.

---

_When we develop unit tests, we must validate that they test all flows. Only in this way is our code completely covered._

---

With the solution open, let's go to `Test > Analyze Code Coverage for All Tests`

![](https://dev-to-uploads.s3.amazonaws.com/i/1yphdpctjjmkjokm0e6j.png)

At each opportunity, the solution will be compiled and the tests run, even if you have already done it previously.

---

_The first time will take longer than usual to get the necessary dependencies._

---

A new window appears: **Code Coverage Results**.

![](https://dev-to-uploads.s3.amazonaws.com/i/b8xrjiurx84p486o0eh9.png)

We agree that the visualization is a bit rustic, although it can be understood (in order from higher to lower) as:

- `.coveragexml` file generated with the coverage report.
- Every project that we have in the solution (where we see extensions `* .dll`).
- Each namespace is registered within each project.
- Each unit test created
- The reference to the method it covers.

On the right, we see numbers, according to blocks or percentage of code covered (according to our preference).

### Exporting to an XML file

Using the button below ("Export Results"), you can export it to a `.coveragexml` file.

![](https://dev-to-uploads.s3.amazonaws.com/i/1b2gqbd6gnk1v8sgxtlh.png)

Its structure is analogous to that of a common XML file, and it can be read programmatically as well as on another machine with Visual Studio installed.

### Visualizing Coverage in Code

When we say that the coverage is intended to give us feedback on the scope of our tests, this is where we can take it to reality.

Simply, we should click on the following button:

![](https://dev-to-uploads.s3.amazonaws.com/i/j3peadgiod14fodnjlpb.png)

To be able to reflect it in our code, with a color according to the status of your coverage.

![](https://dev-to-uploads.s3.amazonaws.com/i/2s30r7kvtmymak1adt0k.png)

We can agree that they are not the most representative colors, but not to complain because we can configure the colors to our liking.

To do this, we go to `Tools > Options > Environment > Fonts and Colors`. Within the window that opens, in "Show settings for", we select **Text Editor**.

![](https://dev-to-uploads.s3.amazonaws.com/i/1mecddeya6j1lco2t47c.png)

Within the same window, in _Display Items_ you will see a long list of options. We are only interested in those that begin with "Coverage...":
- Coverage Not Touched Area
- Coverage Partially Touched Area
- Coverage Touched Area

---

# That's it!

In the next installment, we are going to see this, although with Open Source tools.

_(Versión en español en [Medium](https://medium.com/@rruizdev/analizando-la-cobertura-del-c%C3%B3digo-en-visual-studio-d7e7a240e0f6))_