Publishing Pester Results in Azure DevOps
Once you have configured Pester to generate XML reports (see Pester CI/CD Integration), the next step is to configure your Azure DevOps pipeline to ingest these files so they appear in the "Tests" and "Code Coverage" tabs.
Parent Topic: Pester CI/CD Integration Related: Pester Testing in PowerShell
1. The Pipeline YAML
You need to add two specific tasks to your azure-pipelines.yml file after the step that runs your Pester script.
Task: Publish Test Results
This task reads the NUnit XML file and populates the "Tests" tab in the pipeline run.
- task: PublishTestResults@2
displayName: 'Publish Test Results'
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '**/TestResults.xml'
failTaskOnFailedTests: true
Task: Publish Code Coverage
This task reads the JaCoCo XML file and populates the "Code Coverage" tab.
- task: PublishCodeCoverageResults@2
displayName: 'Publish Code Coverage'
inputs:
summaryFileLocation: '**/coverage.xml'
failIfCoverageEmpty: true
2. Full Pipeline Example
Here is a complete example of a job that runs Pester on PowerShell 7 (pwsh) and publishes the results.
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PowerShell@2
displayName: 'Run Pester Tests'
inputs:
targetType: 'inline'
script: |
# Install Pester if not present
Install-Module Pester -Force -SkipPublisherCheck -Scope CurrentUser
# Run the build script defined in the previous guide
./Build.ps1
pwsh: true
- task: PublishTestResults@2
displayName: 'Publish Test Results'
condition: always() # Run even if tests fail
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '**/Artifacts/TestResults.xml'
failTaskOnFailedTests: true
- task: PublishCodeCoverageResults@2
displayName: 'Publish Code Coverage'
condition: always()
inputs:
summaryFileLocation: '**/Artifacts/coverage.xml'
3. Troubleshooting
- No Results Found: Ensure the paths in
testResultsFilesandsummaryFileLocationmatch exactly where yourBuild.ps1outputs them. - Condition: always(): It is crucial to add
condition: always()to the publish tasks. If Pester detects a failed test, it usually exits with an error code, which would normally stop the pipeline before the results are published.
Return to Pester CI/CD Integration