PowerShell Genie

Run Pester tests and capture results

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

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

1PS> Invoke-Pester -Path .\tests -Output Normal -PassThru | Select-Object Result,FailedCount,PassedCount,TotalCount

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Result FailedCount PassedCount TotalCount
    2 ------ ----------- ----------- ----------
    3 Passed 0 3 3
    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
    1. Stage 1Invoke-Pester-Path -Output -PassThru
    2. Stage 2Select-ObjectPositional 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.

      1. 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

      PowerShell errors and debugging 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.

      Review the lesson

      Continue
      Genie guideRun Pester tests and capture results

      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.