-
It's not an issue, it's more a question, thus opening a new discussion. One of the "dev" goals for this library is to maintain good code quality (including, but not limited to test code coverage >80%). As a contributor, I haven't found an easy way to see the coverage for my code (total coverage, but what is most importantly line coverage to see what is covered and what is not). Maybe I haven't searched well, but I ended up with a custom PS script, which can run tests for a specific library or even a specific class. It opens coverage report in a browser (HTML format) so that you can see actual line coverage for any file. Under the hood, the script uses Here is the code I use <#
.SYNOPSIS
The function runs unit tests for the selected library or class and prints the code coverage report using html format.
.PARAMETER TestProjectName
Test project name, for example PnP.Core.Test. Either TestProjectName or FqdnClassName should be provided.
.PARAMETER FqdnClassName
Fully qualified class name, for example PnP.Core.Test.SharePoint.FilesTests. Either TestProjectName or FqdnClassName should be provided.
.PARAMETER OpenReport
Whether to open a report using the default browser, default is TRUE
.EXAMPLE
.\CoveragePnPCore.ps1 -FqdnClassName PnP.Core.Test.SharePoint.FilesTests
The above command will run all tests in the specified class and will launch a browser with the coverage report
.\CoveragePnPCore.ps1 -TestProjectName PnP.Core.Test -OpenReport $false
The above command will run all tests in the specified test project and will NOT open a browser with the coverage report
#>
param (
[Parameter(Mandatory = $false)]
[string]$TestProjectName,
[Parameter(Mandatory = $false)]
[string]$FqdnClassName,
[Parameter(Mandatory = $false)]
[bool]$OpenReport = $true)
if(!$TestProjectName -and !$FqdnClassName) {
throw "You should provide either -TestProjectName or -FqdnClassName parameter"
return;
}
$projectPath = ""
if($TestProjectName) {
$projectPath = "pnpcore/src/sdk/$($TestProjectName)/$($TestProjectName).csproj"
} else {
$index = $FqdnClassName.IndexOf(".Test.") + ".Test.".Length;
$libName = $FqdnClassName.Substring(0, $index - 1)
$projectPath = "pnpcore/src/sdk/$($libName)/$($libName).csproj"
}
$tempFolder = $env:TEMP
$pnpTestsFolder = "$($tempFolder)/pnp-sdk-tests"
$testResultsPath = "$($pnpTestsFolder)/TestResults"
$coveragePath = "$($pnpTestsFolder)/coverage"
if (Test-Path -Path $pnpTestsFolder) {
Remove-Item -Recurse -Force $pnpTestsFolder
}
if(!$FqdnClassName) {
dotnet test $projectPath --no-restore --verbosity normal --collect:"XPlat Code Coverage" --results-directory $testResultsPath
} else {
dotnet test $projectPath --no-restore --verbosity normal --collect:"XPlat Code Coverage" --results-directory $testResultsPath --filter ClassName=$FqdnClassName
}
& $env:USERPROFILE\.dotnet\tools\reportgenerator.exe -reports:"$($testResultsPath)/**/*.cobertura.xml" -targetdir:$coveragePath -reporttypes:html
$coverageHtml = "$($coveragePath)/index.html"
Write-Host ""
Write-Host "The coverage file is generated under '$($coverageHtml)'" -ForegroundColor Yellow
Write-Host ""
if($OpenReport) {
Invoke-Item $coverageHtml
} So you can run .\CoveragePnPCore.ps1 -FqdnClassName PnP.Core.Test.SharePoint.FilesTests to see coverage for individual test class or the whole library: .\CoveragePnPCore.ps1 -TestProjectName PnP.Core.Test -OpenReport $false This is the sample output from a file coverage where you can click and see the line coverage for a file: Does it make sense to include this script in the repo? It's IDE independent, however runs only on Windows (because of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Great suggestion @s-KaiNet!. Visual Studio Enterprise has built in code coverage tools (see https://visualstudio.microsoft.com/vs/compare/ for version comparison), but not every developer has access to this version. Having an alternative approach is great for folks using VSCode or other IDE's. Would be great if you could add your script under Thanks for helping out! |
Beta Was this translation helpful? Give feedback.
Great suggestion @s-KaiNet!. Visual Studio Enterprise has built in code coverage tools (see https://visualstudio.microsoft.com/vs/compare/ for version comparison), but not every developer has access to this version. Having an alternative approach is great for folks using VSCode or other IDE's. Would be great if you could add your script under
src/tools
and add relevant documentation indocs/contributing/writing%20tests.md
.Thanks for helping out!
FYI: VS 2022 experience: