Measuring code coverage of .NET Core applications with Visual Studio 2017

January 17, 2018 by Anuraj

.NET Core Code coverage Visual Studio

This post is about Measuring code coverage of .NET Core applications with Visual Studio. Test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high test coverage, measured as a percentage, has had more of its source code executed during testing which suggests it has a lower chance of containing undetected software bugs compared to a program with low test coverage.

Code coverage feature in Visual Studio 2017 enabled only in Version 15.3.3 or more.

Code Coverage feature in Visual Studio 2017

Next load your project, click on the Test Menu, Select Analyze Code Coverage menu and select All Tests, which will run all the tests and display code coverage results. I have a BMIService and two unit test cases for it. But if you notice, it is showing code coverage on test cases not the actual code.

Code Coverage results

This is because you of not instrumenting the source code, you can enable it by directly modifying the project file, by adding the following line.

<DebugType>full</DebugType>

Inside the PropertyGroup element after TargetFramework element. Something like this.

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <DebugType>full</DebugType>
</PropertyGroup>

Or you can do this by right clicking on the project, select Properties. Select the Build tab, click on the Advanced Button.

Full option - Debugging Information

And select Full option for Debugging Information from the Advanced Build Settings dialog box. Now you can remove the Full option from Unit Test project, so that it won’t show coverage. After this you can run the Code coverage analysis and can view the results.

Code Coverage with Visual Studio 2017

If you’re using Code coverage in CI build, you can enable it via vstest.console.exe. You need to run vstest.console.exe with --collect:"Code Coverage" argument. Like this.

 "%vsinstalldir%\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" --collect:"Code Coverage" "D:\AspNetCoreSamples\UnitTestDemo\BMICalcService.Tests\bin\Debug\netcoreapp2.0\BMICalcService.Tests.dll"

This will generate a coverage file, and you can open it in Visual Studio to view the results.

Code Coverage with vstest.console.exe

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub