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?
CodePudding user response:
Looks like I'm in luck! XUnit Test Projects come with a NuGet plugin
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:
The results are quite good (for FREE):
You can drill down to see the highlighted line coverage, its just not as powerful or integrated as the Enterprise Edition:
I updated the script to support History as well:
CodePudding user response:
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.