pester

Pester

Pester testing framework conventions for PowerShell BDD-style unit and integration tests

Details

Language / Topic
powershellPowerShell
Category
framework
Compatible Frameworks
pester

Rules

balanced
- Structure every test file with `Describe` > `Context` > `It` blocks; each `It` block tests exactly one behavior and has a descriptive human-readable name.
- Use `Mock` to replace cmdlets with test doubles: `Mock Get-Content { return 'fake' }` — always scope mocks inside `Describe` or `Context` to avoid cross-test contamination.
- Run the full suite with `Invoke-Pester -Path ./tests -Output Detailed -CI` in CI pipelines — the `-CI` flag sets exit code 1 on any failure.
- Use `BeforeAll` for one-time expensive setup (module import, connection init) and `BeforeEach` for per-test state reset to keep tests independent and idempotent.
- Assert mock call counts with `Should -Invoke Get-Content -Times 1 -Exactly` to verify that code under test calls dependencies the expected number of times.
- Use `Should -Throw -ExceptionType [System.IO.FileNotFoundException]` to assert that functions raise the correct exception type on invalid input.
- Organize test tags with `-Tag 'Unit'` and `-Tag 'Integration'` on `Describe` blocks and filter with `Invoke-Pester -Tag Unit` to run only fast tests in PR pipelines.