---
title: Pester Code Coverage
tags: [powershell, pester, testing, code-coverage, quality]
---

# Pester Code Coverage

Code coverage is a metric used to measure the percentage of code that is executed by your automated tests. Pester has built-in support for code coverage analysis.

**Parent Topic:** [[programming/PowerShell/Pester|Pester Testing in PowerShell]]

## 1. Enabling Code Coverage

To measure code coverage, you use the `-CodeCoverage` parameter with `Invoke-Pester`. You pass the path to the script file(s) you want to measure.

```powershell
Invoke-Pester -Path .\MyFunctions.Tests.ps1 -CodeCoverage .\MyFunctions.ps1
```

## 2. Interpreting the Output

Pester will run your tests and then output a code coverage report.

*   **Covered %**: The percentage of commands in the script that were executed.
*   **Missed Commands**: Specific lines or commands that were not run.

Example output:
```text
Tests completed in 200ms
Tests Passed: 2, Failed: 0, Skipped: 0 NotRun: 0
Code coverage report:
Covered 80.00% of 10 analyzed commands in 1 file.
Missed commands:
    MyFunctions.ps1: 15
```

## 3. Improving Coverage

If you have low coverage, look at the "Missed commands" section. It usually points to:
*   `if/else` branches that weren't tested.
*   `catch` blocks for error handling that weren't triggered.
*   Unused functions.

To improve coverage, write new `It` blocks that specifically target those scenarios (e.g., passing invalid input to trigger an error).

---
Return to [[programming/PowerShell/Pester|Pester Testing in PowerShell]]