How to measure Code Coverage in ASP.NET Core projects in Visual Studio?
Add the NuGet package Microsoft.CodeCoverage 1.0.1 to your project.json
.
I am working on Template for Asp.NET
and right now I am working on Unit Tests so I saw your post. You could see project/configuration here.
The codecoverage works for me, for .Net Core using Microsoft.CodeCoverage
as mentioned above.
Check you have Microsoft.CodeCoverage
nuget added to your test project
Also check the project.json file on you main project, the debugType attribute should be "full" instead of "portable"
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"debugType": "full"
},
This fixed for me
Coverlet (https://github.com/tonerdo/coverlet) is a new project that has recently emerged. It works alongside msbuild and gives a straight forward solution for coverage
Disclaimer: These steps were given from Measuring ASP.NET Core Coverage with OpenCover - DotNetThoughts.
Even though the poster says about this site, I thought it would still be best to have these steps here for prosperity.
NOTE: These instructions while tailored to a windows operating system, should easily work for any O/S supported by OpenCover and ReportGenerator.
- Create your ASP.NET Core MVC Website
- Ensure global.json has "test" in projects
- Right click test folder in solution and add a new project
- Make sure the project type is an .NET Core Class library
- Add the following to your project.json dependencies node:
- "dotnet-test-xunit": "2.2.0-preview2-build1029",
- "xunit": "2.2.0-beta3-build3402"
- "Microsoft.CodeCoverage": "1.0.2"
- Add the following to your project.json under version
- "testRunner": "xunit",
- Write your unit tests
- Download OpenCover and Report Generator
- Install OpenCover
- Extract Report Generator into OpenCover install dir in folder called Report Generator
- Create a BAT file in your project and call it cover.bat
- Add the following contents:
@echo off
SET dotnet="C:\Program Files\dotnet\dotnet.exe"
SET opencover="C:\Program Files (x86)\OpenCover\OpenCover.Console.exe"
SET reportgenerator="C:\Program Files (x86)\OpenCover\ReportGenerator\ReportGenerator.exe"
SET targetargs="test"
SET filter="+[*]NAMESPACE.* -[*.Test]* -[xunit.*]* -[FluentValidation]*"
SET coveragefile=Coverage.xml
SET coveragedir=Coverage
REM Run code coverage analysis
%opencover% -oldStyle -register:user -target:%dotnet% -output:%coveragefile% -targetargs:%targetargs% -filter:%filter% -skipautoprops -hideskipped:All
REM Generate the report
%reportgenerator% -targetdir:%coveragedir% -reporttypes:Html;Badges -reports:%coveragefile% -verbosity:Error
REM Open the report
start "report" "%coveragedir%\index.htm"
- Replace the NAMESPACE with your projects namespace.
- If more than one project, duplicate the regex
+[*]NAMESPACE.*
as many times as needed for each namespace - Save the file
- Open a command prompt and ensure in your test project
- Type cover to get your unit tests running and your coverage results in HTML format, or whatever you named your bat file in step 11.