Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure Code Coverage in ASP.NET Core projects in Visual Studio?

I want to measure the Code Coverage of my XUnit-Tests in an ASP.NET Core application. The Tooling for .NET Core in Visual Studio 2015 is preview 2 and code coverage does not work so far.

The blog post http://dotnetthoughts.net/measuring-code-coverage-of-aspnet-core-applications-using-opencover/ from February shows a workaround by using the command line of open cover. I am looking for a more integrated way inside of Visual Studio.

Has anybody heard of a better / more integrated way of measuring Code Coverage in combination with XUnit ?

like image 649
Ralf Bönning Avatar asked Jul 17 '16 21:07

Ralf Bönning


People also ask

How do I find code coverage code in Visual Studio?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.

How is code coverage measured in C#?

Code coverage is to determine to what portion of your project code is being tested by Unit testing; you can use the code coverage feature of Visual Studio. Code coverage is an option, when you run the test methods, using Test Explorer. On the Test menu, choose Analyze Code Coverage.

Which technique is used to measure code coverage?

Code Coverage Analysis is simply a structural testing technique to measure how many lines/blocks/arcs of your implemented code are executed while the automated tests are running. Its analysis gives you a quick, automated and accurate quality & coverage measurement for test plans.


2 Answers

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.

like image 141
adenial Avatar answered Oct 06 '22 00:10

adenial


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.

  1. Create your ASP.NET Core MVC Website
  2. Ensure global.json has "test" in projects
  3. Right click test folder in solution and add a new project
  4. Make sure the project type is an .NET Core Class library
  5. 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"
  6. Add the following to your project.json under version
    • "testRunner": "xunit",
  7. Write your unit tests
  8. Download OpenCover and Report Generator
  9. Install OpenCover
  10. Extract Report Generator into OpenCover install dir in folder called Report Generator
  11. Create a BAT file in your project and call it cover.bat
  12. 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"
  1. Replace the NAMESPACE with your projects namespace.
  2. If more than one project, duplicate the regex +[*]NAMESPACE.* as many times as needed for each namespace
  3. Save the file
  4. Open a command prompt and ensure in your test project
  5. 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.
like image 22
Wayne Avatar answered Oct 06 '22 00:10

Wayne