PowerShell Genie

Pester module: setup and commands

Module 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Write repeatable PowerShell tests with Describe, It and Should, then isolate dependencies with mocks that target the command actually called.

Getting it

A Pester 3.4 copy ships with Windows, so install the current release side by side using Install-PSResource -Name Pester and import the version you want explicitly.

Examples

1PS> Invoke-Pester -Path .\Tests -Output Detailed
1PS> Starting discovery in 1 files.
2PS> Discovery found 3 tests.
3PS> [+] Tests.ps1 312ms
4PS> Tests completed in 312ms
5PS> Tests Passed: 3, Failed: 0, Skipped: 0
Show reference output
1 Describe "Get-ServerHealth" { It "returns a computer name" { (Get-ServerHealth).Computer | Should -Not -BeNullOrEmpty } }
1PS> Describing Get-ServerHealth
2PS> [+] returns a computer name 42ms
3PS> Tests completed in 42ms
4PS> Tests Passed: 1, Failed: 0

What it is for

Use it to pin down behavior on functions and modules before refactoring them. It also makes infrastructure assertions practical, since a test can check that a service is running or a certificate is still valid.

Typical tasks

  • Assert the shape and values of function output
  • Mock a cmdlet so tests never touch production
  • Fail a CI build when a regression appears
  • Produce NUnit XML for a test report
  • Measure code coverage across a module
  • Validate infrastructure state as a test

Check the installed version

1PS> Get-Module -ListAvailable Pester | Select-Object Name, Version, Path
2PS> Get-Command -Module Pester

Compare the module path and version with the session used by your scheduled task or automation account. Use it to pin down behavior on functions and modules before refactoring them. It also makes infrastructure assertions practical, since a test can check that a service is running or a certificate is still valid.

Primary documentation

Pester documentation

Continue
Genie guidePester module: setup and commands

Write repeatable PowerShell tests with Describe, It and Should, then isolate dependencies with mocks that target the command actually called.