Wednesday, January 3, 2018

.NetCore - How to enable Code Coverage in Visual Studio 2017

In my previous post I discussed how to enable code analysis in .Net Core. One of the other tools that I used in the .Net Framework world was Code Coverage. I like to have a sense of how much code my unit tests cover. This is one of many metrics I like to look at to get a sense of code quality. 

Creating a Unit Test project is simple and documented many places so I won't duplicate that here. What I want to cover is what you need to do to a .Net Core project to enable Code Coverage results to be displayed. Code Coverage requires a classic Windows PDB file as opposed to the new format (click here for details of why a new format was created.) To do this you need to add the DebugType tag to the project with a value of Full

<DebugType>Full</DebugType>

This will cause the project to generate a classic Windows pdb so that the Code Coverage tools in Visual Studio will work correctly. It is often, almost always, useful to also create a runsettings file so that the test project doesn't display in the code coverage report and so that you can tweak it to only show your code. Details on creating a runsettings file, including a sample can be found here

Lastly, there are a lot of posts on the internet about the evils of a full pdb in release mode (optimized) builds. This StackOverflow post incorrectly states that there is a performance impact in one of the comments. In Native code and in .Net Framework 4.0 and above and in .Net Core there is NO performance impact of creating a full pdb. Here is a Wintellect Post by John Robbins with the details. This Stack Overflow post sums the article up nicely, it is what led me to John Robbin's Post.

No comments:

Post a Comment

The 2024 State of DevOps Report and the Importance of Internal Development Platforms

On the State of DevOps Report The State of DevOps Report, published annually by the DevOps Research and Assessment (DORA) team, has been a c...