Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
getting-code-coverage.md173 linesDownload Raw Back to testing-guidelines
1# Getting Code Coverage Analysis for PowerShell2 3**Note: Code coverage is currently only supported on Windows, since we use OpenCover.**4 5The PowerShell code base is configured to build with code coverage support using [OpenCover].6 7You can see the testing coverage of the current [`master`] branch build at any time at [codecov.io].8 9To run test coverage analysis of PowerShell on your own branch/machine,10you will need to take the following steps11(and be aware that running the code coverage analysis can take as long as 8 hours).12 13## Running tests with code coverage analysis14 15**First**: Open PowerShell in an **elevated** session.16OpenCover needs elevated privileges to work.17 18Now, in PowerShell:19 20```powershell21# Go to your PowerShell build directory root22PS> Set-Location "C:\Path\to\powershell\build\dir"23 24# Import the PowerShell build module25PS> Import-Module .\build.psm126 27# Build PowerShell. You may need to add other flags here like28# -ResGen or -Restore29PS> Start-PSBuild -Configuration CodeCoverage -Clean -PsModuleRestore30 31# Now ensure Pester is installed32PS> Restore-PSPester33 34# We also need to build the test executor35PS> Publish-PSTestTools36 37# Import the OpenCover module38PS> Import-Module $PWD\test\tools\OpenCover39 40# Install OpenCover to a temporary directory41PS> Install-OpenCover -TargetDirectory $env:TEMP -Force42 43# Finally, run the tests with code coverage analysis.44# If you want to run only the continuous integration tests,45# add -CIOnly, which will take less time46PS> Invoke-OpenCover -OutputLog coverage.xml -OpenCoverPath $env:TEMP\OpenCover47```48 49## Examining the code coverage data50 51Once the code coverage test run is done, you'll want to examine the data:52 53```powershell54# Collect the coverage data using Get-CodeCoverage from the OpenCover55# module that was imported above. This operation is generally expensive56# to compute, so worth storing in a variable57PS> $coverageData = Get-CodeCoverage .\coverage.xml58 59# Take a look at a summary of the results60PS> $coverageData.CoverageSummary61 62NumSequencePoints       : 29823763VisitedSequencePoints   : 12594964NumBranchPoints         : 10147765VisitedBranchPoints     : 3938966SequenceCoverage        : 42.2367BranchCoverage          : 38.8268MaxCyclomaticComplexity : 39369MinCyclomaticComplexity : 170VisitedClasses          : 199071NumClasses              : 318772VisitedMethods          : 1511573NumMethods              : 3251774 75# You can also view results by assembly76PS> $coverageData.Assembly | Format-Table AssemblyName,Branch,Sequence77 78AssemblyName                              Branch Sequence79------------                              ------ --------80pwsh                                      100    10081Microsoft.PowerShell.ConsoleHost          21.58  23.3282System.Management.Automation              41.22  45.0183Microsoft.PowerShell.CoreCLR.Eventing     1.88   2.0384Microsoft.PowerShell.Security             17.32  20.0985Microsoft.PowerShell.Commands.Utility     20.14  21.3986Microsoft.PowerShell.Commands.Management  43.05  43.3987Microsoft.WSMan.Management                52.58  56.9888Microsoft.WSMan.Runtime                   80.95  80.3389Microsoft.PowerShell.Commands.Diagnostics 0      090```91 92If you have made changes to tests or code93and run a second code coverage run,94you can also compare code coverage results:95 96```powershell97PS> $cov1 = Get-CodeCoverage ./coverage1.xml98PS> $cov2 = Get-CodeCoverage ./coverage2.xml99PS> Compare-CodeCoverage -Run1 $cov1 -Run2 $cov2100 101AssemblyName                              Sequence SequenceDelta Branch BranchDelta102------------                              -------- ------------- ------ -----------103Microsoft.PowerShell.Security                20.09        -30.12  17.32      -31.63104Microsoft.PowerShell.Commands.Management     43.39          9.10  43.05       11.59105System.Management.Automation                 45.04        -10.63  41.23      -11.07106Microsoft.PowerShell.Commands.Utility        21.39        -47.22  20.14      -46.47107Microsoft.PowerShell.Commands.Diagnostics        0        -51.91      0      -48.62108Microsoft.PowerShell.ConsoleHost             23.32        -22.28  21.58      -22.47109pwsh                                           100          0.00    100        0.00110Microsoft.WSMan.Management                   57.73         48.23  53.02       43.22111Microsoft.WSMan.Runtime                      80.33        -19.67  80.95      -19.05112Microsoft.PowerShell.CoreCLR.Eventing         2.03        -32.74   1.88      -26.01113```114 115To get file-specific coverage data,116you can use `Compare-FileCoverage`:117 118```powershell119PS> Compare-FileCoverage -ReferenceCoverage $cov2 -DifferenceCoverage $cov1 -FileName LanguagePrimitives.cs120 121FileName              ReferenceCoverage DifferenceCoverage CoverageDelta122--------              ----------------- ------------------ -------------123LanguagePrimitives.cs 53.68             69.03              15.34124```125 126You can see more ways to use `Compare-CodeCoverage` and `Compare-FileCoverage`127by running:128 129```powershell130PS> Get-Help Compare-CodeCoverage -Full131# Or132PS> Get-Help Compare-FileCoverage -Full133```134 135## Visualizing code coverage136 137For a more detailed, graphical representation of the code coverage results,138you can use the ReportGenerator package.139This generates an HTML report of the coverage from the XML file140and will provide much more detail about the coverage analysis.141The package is available on [NuGet],142and you can install and run it as follows:143 144```powershell145# Install ReportGenerator146PS> Find-Package ReportGenerator `147>> -ProviderName Nuget `148>> -Source "https://nuget.org/api/v2" `149>> | Install-Package -Scope CurrentUser150 151# Get the ReportGenerator executable path152# Make sure use the appropriate version number in the path153$ReportGenExe = "$HOME\AppData\Local\PackageManagement\NuGet\Packages\ReportGenerator.<version>\tools\ReportGenerator.exe"154 155# Run ReportGenerator156& $ReportGenExe -report:coverage.xml -targetdir:C:\temp\Coverage157 158# Finally, open the report in your browser159Invoke-Item C:\temp\Coverage\index.htm160```161 162This should open a screen in the browser like this:163![Coverage report browser page](Images/CoverageReportTop.PNG)164 165The main report, which is below the summary and risk hot spots, has166a filter functionality as well (when "Enable Filtering" is clicked on):167![Coverage report with filter on](Images/CoverageReportFilter.PNG)168 169[OpenCover]: https://github.com/OpenCover/opencover170[codecov.io]: https://codecov.io171[`master`]: https://github.com/PowerShell/PowerShell172[NuGet]: https://nuget.org/packages/ReportGenerator173