How to get Code Coverage from Unit Tests in Visual Studio 2022 Community Edition?

Viewed 17166

I've downloaded the latest VS2022 v17.1 Community Edition and it doesn't come with Code Coverage in-built. I'm accustomed to the Enterprise Edition and all I can find is paid options for the Community Edition.

Is it possible to do Code Coverage in VS2022 Community Edition for FREE?

enter image description here

3 Answers

You have Fine Code Coverage working with VS 2022, you can access to it here https://github.com/FortuneN/FineCodeCoverage/releases and click on the 2022 file.

After that, it´s just a plugin that you install on your computer and it´s available to every single project without the need to add it project by project.

The Server Build with History way...

XUnit Test Projects come with a NuGet plugin Coverlet.Collector:

enter image description here

This doesn't need to be installed in any project, all you need to do is run these steps that I've made into a Powershell script:

ExecCodeCoverage.ps1

# PURPOSE: Automates the running of Unit Tests and Code Coverage
# REF: https://stackoverflow.com/a/70321555/495455

# If running outside the test folder
#cd E:\Dev\XYZ\src\XYZTestProject

# This only needs to be installed once (globally), if installed it fails silently: 
dotnet tool install -g dotnet-reportgenerator-globaltool

# Save currect directory into a variable
$dir = pwd

# Delete previous test run results (there's a bunch of subfolders named with guids)
Remove-Item -Recurse -Force $dir/TestResults/

# Run the Coverlet.Collector (this is an NuGet included with XUnit Test Projects)
$output = [string] (& dotnet test --collect:"XPlat Code Coverage" 2>&1)
Write-Host "Last Exit Code: $lastexitcode"
Write-Host $output

# Extract the GUID from the Output eg, 
#"Attachments:   E:\Dev\XYZ\src\XYZTestProject\TestResults\0f26f16d-bbe8-463b-856b-6d4fbee673bd\coverage.cobertura.xml Passed!"  

$i = $output.LastIndexOf("TestResults") + 11
$j = $output.LastIndexOf("coverage")
$cmdGuid = $output.SubString($i,$j - $i - 1)
Write-Host $cmdGuid 

# Delete previous test run reports - note if you're getting wrong results do a Solution Clean and Rebuild to remove stale DLLs in the bin folder
Remove-Item -Recurse -Force $dir/coveragereport/

# To keep a history of the Code Coverage we need to use the argument:
# -historydir:SOME_DIRECTORY 
if (!(Test-Path -path $dir/CoverageHistory)) {  
 New-Item -ItemType directory -Path $dir/CoverageHistory
}

# Generate the Code Coverage HTML Report
reportgenerator -reports:"$dir/TestResults/$cmdGuid/coverage.cobertura.xml" -targetdir:"coveragereport" -reporttypes:Html -historydir:$dir/CoverageHistory 

# Open the Code Coverage HTML Report (if running on a WorkStation)
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
if ($osInfo.ProductType -eq 1) {
(& "$dir/coveragereport/index.html")
}

Put it in the TestProject:

enter image description here

The results are quite good (for FREE):

enter image description here

You can drill down to see the highlighted line coverage, its just not as powerful or integrated as the Enterprise Edition:

enter image description here

I updated the script to support History as well:

enter image description here

I've had some problems with Visual Studio extensions, so I ended up using my very best friend, the command line.

You can do from the command line, using Microsoft's dotnet-coverage and danielpalme dotnet-reportgenerator-globaltool

I believe this should work with any .Net core runtime and VS version, and also on CI servers (I've tested .Net 5)

  • Install (run as admin)
dotnet tool install -g dotnet-coverage
dotnet tool install -g dotnet-reportgenerator-globaltool
  • Run tests with XML output format:
dotnet-coverage collect -f xml -o coverage.xml dotnet test <solution/project>
  • Generate html report
reportgenerator -reports:coverage.xml -targetdir:.\report -assemblyfilters:+MyTestedAssembly.dll
  • Open report\index.html
Related