---
title: Pester CI/CD Integration and XML Reports
tags: [powershell, pester, ci-cd, xml, devops, testing]
---

# Pester CI/CD Integration and XML Reports

Integrating Pester tests into a CI/CD pipeline (like Azure DevOps, GitHub Actions, or GitLab CI) requires generating machine-readable reports. Pester 5+ (standard in PowerShell 7+) uses a configuration object to handle this efficiently.

**Parent Topic:** [[programming/PowerShell/Pester|Pester Testing in PowerShell]]
**Related:** [[programming/PowerShell/PesterCodeCoverage|Pester Code Coverage]]

## 1. The Pester Configuration Object

For advanced scenarios like CI/CD, `Invoke-Pester` with simple parameters isn't enough. You should use `New-PesterConfiguration`.

```powershell
$config = New-PesterConfiguration
$config.Run.Path = ".\Tests"
$config.Run.Exit = $true
$config.Output.Verbosity = "Detailed"
```

## 2. Generating Test Results (NUnit/JUnit XML)

Most CI platforms consume NUnit or JUnit XML files to display test results in their UI.

```powershell
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = "NUnitXml"
$config.TestResult.OutputPath = ".\TestResults.xml"
```

## 3. Generating Code Coverage Reports (JaCoCo XML)

To visualize code coverage in tools like Codecov or Azure DevOps, you need a coverage report, typically in JaCoCo format.

```powershell
$config.CodeCoverage.Enabled = $true
$config.CodeCoverage.OutputFormat = "JaCoCo"
$config.CodeCoverage.OutputPath = ".\coverage.xml"
$config.CodeCoverage.Path = ".\Src" # Path to source files to measure
```

## 4. Complete CI Script Example

Here is a robust script (`Build.ps1`) suitable for a CI pipeline running on PowerShell 7+.

```powershell
# Build.ps1

# 1. Create Configuration
$config = New-PesterConfiguration

# 2. Configure Run
$config.Run.Path = ".\Tests"
$config.Run.Exit = $true # Exit with non-zero code if tests fail

# 3. Configure Test Results (NUnit XML)
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = "NUnitXml"
$config.TestResult.OutputPath = ".\Artifacts\TestResults.xml"

# 4. Configure Code Coverage (JaCoCo XML)
$config.CodeCoverage.Enabled = $true
$config.CodeCoverage.OutputFormat = "JaCoCo"
$config.CodeCoverage.OutputPath = ".\Artifacts\coverage.xml"
$config.CodeCoverage.Path = ".\Src"

# 5. Execute
Invoke-Pester -Configuration $config
```

---
Return to [[programming/PowerShell/Pester|Pester Testing in PowerShell]]