Migrate Pester tests from v4 to v5#27290
Draft
nohwnd wants to merge 2 commits intoPowerShell:masterfrom
Draft
Conversation
Migrate the PowerShell test suite from Pester 4.99 to Pester 5.7.1. 207 test files updated for Pester 5's discovery/run execution model, plus build.psm1 and tools/ci.psm1 for the version bump and PesterConfiguration-based invocation. No test logic, assertions, or expected behaviors were changed. Key migration patterns applied across 207 test files: - Add BeforeDiscovery blocks for test-case data (260 blocks) - Move setup code from script scope into BeforeAll (236 blocks) - Remove duplicate variable assignments (49 files) - Fix test hangs from Pester 5 stricter execution (4 hangs resolved) - Replace ping with pwsh in Start-Process.Tests (firewall popups) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
c019447 to
eeb6249
Compare
The PassThru property was added as a second Run key, causing 'Duplicate keys are not allowed in hash literals' error. Merged PassThru into the single Run block. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
BeforeDiscovery/BeforeAllblocks for Pester 5's execution model.Why
Pester 4 is no longer maintained. This updates PowerShell's test infrastructure to the supported framework version and aligns the suite with Pester 5's discovery/execution model.
Before → After
Both baselines captured on Windows, same machine, same environment, 600s per-file timeout.
P5 runs 568 more tests and resolves 3 of 4 P4 hangs. The higher failed/skipped counts come from tests that P4 couldn't enumerate now being properly discovered — many are environment-dependent (remoting, services, admin rights) and correctly skip or fail. 342 files have identical test counts between P4 and P5.
Tests P4 couldn't discover (returned 0 or 1) — now working in P5
What if:output instead of resultTestDrive:reference issueFiles where P5 has fewer tests (2 files, −62)
Verify()function replaced with explicitItblocks and soft assertions. All breakpoint validation logic preserved.pingwithpwshto avoid firewall popups.Hang resolution
Wait-Processwithout-TimeoutonpingWhat changed
Build / CI (2 files)
build.psm1MaximumVersion 4.99→RequiredVersion 5.7.1;Invoke-Pesterrewritten to use[PesterConfiguration]tools/ci.psm1MaximumVersion 4.99→MinimumVersion 5.0; samePesterConfigurationpatternTest files (207 files)
The dominant migration pattern adapts to Pester 5's discovery/run phase separation:
Migration pattern example (click to expand)
Applied across 207 files:
BeforeDiscoveryblocksBeforeAllblocksSetupremovalSetup -File/-DirectorywithNew-Item/Set-Contenttargeting$TestDrivereturn→Set-ItResultreturninItas silent pass; replaced withSet-ItResult -Skippedtry/catch→Should -Throwtry/finally→AfterAll$PSDefaultParameterValues['It:Skip']→-Skip:-Skipat discovery, not runtimeWhat did NOT change
Should -Be,Should -Throw, etc.) — unchangedScope by area
Possible improvements (not in this PR)
-Timeout 30toWait-Processcalls would fix it.