Run Pester tests and capture results
Documentation: Microsoft Learn
Invoke-Pester executes the test files under the tests directory with normal console output, and -PassThru returns the run object instead of only printing to the console.
The script
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Invoke-Pester -Path .\tests -Output Normal -PassThru | Select-Object Result,FailedCount,PassedCount,TotalCount- Stage 1Invoke-Pester
-Path -Output -PassThru - Stage 2Select-Object
Positional values or no named parameters
How it works
Invoke-Pester executes the test files under the tests directory with normal console output, and -PassThru returns the run object instead of only printing to the console. The projection reads the overall Result along with the failed, passed, and total counts.
Adapting it
Add -CI so the run emits a NUnit XML file and sets a non-zero exit code on failure, which is the behavior most build systems are already looking for.
Requirements
Pester 5 or later installed and imported.
If the result is unexpected
Pester reports success despite missing tests or unavailable result data.
Discovery paths, tags, test configuration and result-output options vary by version.
- Confirm the Pester version and inspect discovered, executed and failed test counts.
Validate the result
The intended tests ran and their result object or report records the expected counts.
Keep the check reliable
Fail the workflow when required tests are not discovered as well as when executed tests fail.
Primary documentation
Practise the script’s building blocks
Pass the supplied service tests
Define Get-ServiceSummary returning Name and Status, import Pester, then run the supplied test file with -PassThru. All tests must pass.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.