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

