Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generate code coverage with coverlet and reportgenerator when there are many assemblies
param (
[string]$format = "opencover",
[bool]$nohtml = $false
if (-Not (Get-Command -Name reportgenerator -ErrorAction SilentlyContinue))
Write-Output "Installing reportgenerator"
Invoke-Expression "dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.0.9"
function Get-BaseCommand {
Param ([string]$basename, [string]$name)
return "dotnet test ./testes/$($BaseName)/$($Name) /p:Exclude='[xunit.*]*' /p:CollectCoverage=true";
Write-Output "Getting csproj files"
$reports = [System.Collections.ArrayList]@()
$count = Get-ChildItem -Path .\testes -Recurse -Filter *.csproj -File | Measure-Object | ForEach-Object{$_.Count}
Get-ChildItem -Path .\testes -Recurse -Filter *.csproj -File | ForEach-Object {
Write-Output "Running tests of project $($_.BaseName)/$($_.Name)"
if ($reports.Count -eq $count-1) {
Write-Output "Generating unified report using $($format) format"
Invoke-Expression "$(Get-BaseCommand -basename $_.BaseName -name $_.Name) /p:MergeWith='..\result.json' /p:CoverletOutput='..\coverage.$($format).xml' /p:CoverletOutputFormat='$($format)'"
elseif ($reports.Count -gt 0) {
Write-Output "Merging results coverage with project $($reports[$reports.Count-1])"
Invoke-Expression "$(Get-BaseCommand -basename $_.BaseName -name $_.Name) /p:MergeWith='..\result.json' /p:CoverletOutput='..\result.json'"
} else {
Invoke-Expression "$(Get-BaseCommand -basename $_.BaseName -name $_.Name) /p:CoverletOutput='..\result.json'"
$reportsText = $reports -join "`n"
Write-Output "Run tests finished. Tested projects: `n$($reportsText)"
if ($nohtml -eq $false) {
Invoke-Expression "reportgenerator '-reports:./testes/coverage.opencover.xml' '-targetdir:./report'"
Invoke-Item ./report/index.htm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment