Ever since Pester 5 a Pester configuration is recommended to generate test and code coverage output. The old school Pester 4 method still works, but we wanted to get rid of the legacy warning. Since we started to use Pester Configuration, Azure DevOps no longer shows the preview files where are highlighted.
The error looks like this;
My PowerShell code:
$pesterConfiguration = @{
Run = @{
Path = '.\Interfaces'
}
Should = @{
ErrorAction = 'Continue'
}
CodeCoverage = @{
CodeCoveragePath = $codeCoveragePath
OutputFormat = 'JaCoCo'
OutputEncoding = 'UTF8'
OutputPath = ".\Pester-Coverage.xml"
Enabled = $true
}
TestResult = @{
OutputPath = ".\Pester-Test.xml"
OutputFormat = 'NUnitXml'
OutputEncoding = 'UTF8'
Enabled = $true
}
}
#Invoke pester with the configuration hashtable
$config = New-PesterConfiguration -Hashtable $pesterConfiguration
Invoke-Pester -Configuration $config
Azure DevOps yaml to upload the code coverage
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'JaCoCo'
summaryFileLocation: '**/Pester-Coverage.xml'
pathToSources: $(System.DefaultWorkingDirectory)
failIfCoverageEmpty: true
With the old-school Pester 4 method, the code coverage works fine.
Invoke-Pester -Script $TestFiles -CodeCoverage $NotTestFiles -OutputFile $OutFile -OutputFormat NUnitXml -CodeCoverageOutputFile $OutCoverageFile
I'm using Pester 5.3.1
in combination with Powershell Core 7.2.2
Did anyone crack this case and managed to fix this annoyance?
We managed to solve this issue by altering the Pester-Coverage.xml file after it was created. Add this code directly under the Invoke-Pester -Configuration $config
command.
[xml]$pesterCoverageOut = get-content -path ".\Pester-Coverage.xml"
foreach ($classNode in $pesterCoverageOut.SelectNodes("//class")) {
$classNode.sourcefilename = "Interfaces/$($classNode.sourcefilename)"
}
foreach ($sourceFileNode in $pesterCoverageOut.SelectNodes("//sourcefile")) {
$sourceFileNode.name = "Interfaces/$($sourceFileNode.name)"
}
$pesterCoverageOut.Save(".\Pester-Coverage.xml")
A bug report with this solution is created at the Pester github. https://github.com/pester/Pester/issues/2149