Pester is a PowerShell framework for repeatable tests and mocked command behavior. Use it to protect reusable functions and modules before changing code: unit tests check a function’s logic, while integration tests confirm that pieces work together. Tests can also preserve results for reporting and historical review. section 13.0 section 13.6
Install Pester 5.3.1 or later when the default 3.4.0 version is present. Pester organizes tests with keywords rather than cmdlets: Describe names a logical suite, Context groups related cases, It contains one self-contained test, and Should asserts the expected result. Save tests in a .ps1 file; they run as a script rather than interactively. section 13.1
Describe "Boolean Test" {
Context "True Tests" {
It '$true is true' {
$true | Should -Be $true
$true | Should -Not -Be $false
$true | Should -BeTrue
$true | Should -BeOfType [System.Boolean]
}
}
Context "False Tests" {
It '$false is false' {
$false | Should -Be $false
$false | Should -Not -Be $true
$false | Should -BeFalse
$false | Should -BeOfType [System.Boolean]
}
}
}
This Boolean suite shows the nesting of Describe, Context, and It, plus several Should assertions for values, negation, Boolean state, and type. section 13.1
An It block does not share variables or commands with another It block. Put common setup in BeforeAll when it should run once, or BeforeEach when every test needs a fresh setup. A top-level BeforeAll can load a custom function into the session so all following suites can call it. section 13.1 section 13.2.1
Testing a hotfix-status function
Load Get-HotFixStatus in BeforeAll, then test a known installed KB for $true, an absent KB for $false, and an unreachable computer for a terminating error with Should -Throw. These local checks depend on machine state, so they can produce a false failure elsewhere; mocks make unit tests consistent by replacing command behavior. section 13.2.2 section 13.6
Listing 13.2 Get-HotFixStatus.Unit.Tests.ps1 local check
BeforeAll {
Set-Location -Path $PSScriptRoot #1
. .\Get-HotFixStatus.ps1
}
Describe 'Get-HotFixStatus' { #2
It "Hotfix is found on the computer" {
$KBFound = Get-HotFixStatus -Id 'KB5011493' -Computer 'localhost'
$KBFound | Should -Be $true
}
It "Hotfix is not found on the computer" {
$KBFound = Get-HotFixStatus -Id 'KB1234567' -Computer 'localhost'
$KBFound | Should -Be $false
}
It "Unable to connect" {
{ Get-HotFixStatus -Id 'KB5011493' -Computer 'Bad' } | Should -Throw
}
}
The complete local-check script combines setup, a named suite, positive and negative hotfix cases, and an expected connection error. section 13.2.2
When maintaining a module, test existing behavior before extending it. For Get-SystemInfo, assertions first check that expected properties contain data; a mock of Get-CimInstance can then return an exported CLIXML object for stable value assertions. CLIXML retains object type information when imported, helping the mock resemble the original object. section 14.1 section 14.1.1
Invoke-Pester runs every .Tests.ps1 file in the current folder. Create a configuration with New-PesterConfiguration to control output and write results to an NUnit-schema test-results file, allowing automated test runs to be reported. section 13.5

