1+ # ###################################################################################
2+ # To execute
3+ # 1. In powershell, set security polilcy for this script:
4+ # Set-ExecutionPolicy Unrestricted -Scope Process -Force
5+ # 2. Change directory to the script folder:
6+ # CD src (wherever your script is)
7+ # 3. In powershell, run script:
8+ # .\Get-CodeCoverage.ps1 -TestProjectFilter 'MyTests.*.csproj' -ProdPackagesOnly -ProductionAssemblies 'MyApp.Core','MyApp.Web'
9+ # This script is for local use to analyze code coverage in more detail using HTML report.
10+ # ###################################################################################
11+
12+ Param (
13+ [string ]$TestProjectFilter = ' Tests.*.csproj' ,
14+ [switch ]$ProdPackagesOnly = $false ,
15+ [string []]$ProductionAssemblies = @ (
16+ " Goodtocode.SemanticKernel.Core.Application" ,
17+ " Goodtocode.SemanticKernel.Presentation.WebApi" ,
18+ " Goodtocode.SemanticKernel.Presentation.Blazor"
19+ )
20+ )
21+ # ###################################################################################
22+ Set-ExecutionPolicy Unrestricted - Scope Process - Force
23+ $VerbosePreference = ' SilentlyContinue' # 'Continue'
24+ # ###################################################################################
25+
26+ & dotnet tool install - g coverlet.console
27+ & dotnet tool install - g dotnet- reportgenerator- globaltool
28+
29+ $timestamp = Get-Date - Format " yyyyMMdd-HHmmss"
30+ $scriptPath = Get-Item - Path $PSScriptRoot
31+ $coverageOutputPath = Join-Path $scriptPath " TestResults\Coverage\$timestamp "
32+ $reportOutputPath = Join-Path $scriptPath " TestResults\Reports\$timestamp "
33+
34+ New-Item - ItemType Directory - Force - Path $coverageOutputPath
35+ New-Item - ItemType Directory - Force - Path $reportOutputPath
36+
37+ # Find tests for projects with 'Tests.*.csproj'
38+ $testProjects = Get-ChildItem $scriptPath - Filter $TestProjectFilter - Recurse
39+ Write-Host " Found $ ( $testProjects.Count ) test projects."
40+ foreach ($project in $testProjects ) {
41+ $testProjectPath = $project.FullName
42+ Write-Host " Running tests for project: $ ( $testProjectPath ) "
43+
44+ $buildOutput = Join-Path - Path $project.Directory.FullName - ChildPath " bin\Debug\net9.0\$ ( $project.BaseName ) .dll"
45+ $coverageFile = Join-Path $coverageOutputPath " coverage.cobertura.xml"
46+ Write-Host " Analyzing code coverage for: $buildOutput "
47+ coverlet $buildOutput -- target " dotnet" -- targetargs " test $ ( $project.FullName ) --no-build" -- format cobertura -- output $coverageFile
48+
49+ }
50+
51+ # Generate HTML report
52+ if ($ProdPackagesOnly ) {
53+
54+ $assemblyFilters = ($ProductionAssemblies | ForEach-Object { " +$_ " }) -join " ;"
55+ & reportgenerator - reports:" $coverageOutputPath /**/coverage.cobertura.xml" - targetdir:$reportOutputPath - reporttypes:Html - assemblyfilters:$assemblyFilters
56+ }
57+ else {
58+ & reportgenerator - reports:" $coverageOutputPath /**/coverage.cobertura.xml" - targetdir:$reportOutputPath - reporttypes:Html
59+ }
60+
61+ Write-Host " Code coverage report generated at: $reportOutputPath "
62+
63+ $reportIndexHtml = Join-Path $reportOutputPath " index.html"
64+ Invoke-Item - Path $reportIndexHtml
0 commit comments