liveBook · Manning

Pester

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Pester is a PowerShell testing framework for authoring and running automated tests of commands, scripts, and functions.

Pester is an open-source PowerShell framework for authoring and running automated tests of commands, scripts, and functions. It helps catch regressions before changed or unusual conditions reach production. section 20.0 section 20.4

A practical Pester workflow looks like this:

  • Design self-contained, single-task PowerShell tools so each behavior has a manageable set of tests. section 20.5
  • Create a fixture containing a code script and a matching .Tests.ps1 file, then load the code under test. section 20.7.1
  • Group related tests in Describe blocks and express individual pass-or-fail checks with It blocks and Should assertions. section 20.7.1 section 20.7.2
  • Mock commands that should appear to run without performing their real side effects, such as restarting a computer. section 20.7.3
  • Verify calls with Should -Invoke, using -Scope It when each test must count mock invocations independently. section 20.7.4
  • Run tests with Invoke-Pester and inspect code coverage to find untested paths, while remembering that coverage alone does not prove completeness. section 20.7.3 section 20.7.5

Testing Set-ComputerState

A fixture for Set-ComputerState mocks Restart-Computer and Stop-Computer so tests can check behavior without restarting or stopping machines. Tests cover one computer, multiple computer names, pipeline input, forced restarts, and shutdowns; Should -Invoke verifies the expected call count. section 20.7.1 section 20.7.5

PS C:> Import-Module Pester
PS C:> Mkdir example
PS C:> New-Fixture –Path example –Name Set-ComputerState

Use the fixture commands to import Pester, create an example directory, and generate a Set-ComputerState test skeleton. section 20.7.1

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains fixtures, assertions, mocks, invocation scopes, and coverage, but stops short of comprehensive integration testing and advanced features.
Examples
Uses a Set-ComputerState fixture to test restart, shutdown, force, multiple inputs, pipeline input, mocks, call counts, and coverage.
What it assumes
Assumes PowerShell command and script foundations while teaching Pester’s basic test structure, mocking, invocation checks, and coverage from the beginning.
What you can do after
Apply it via Pester to build fixtures, isolate side effects with mocks, run tests, verify calls, and locate missed code paths.
How it connects
Connects PowerShell commands with unit testing, integration testing, mocking, assertions, and code coverage.

Also explained in

Compare how 2 books explain pester →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.