You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Pester Set-ItResult Pattern for Pending and Skipped Tests
7
+
8
+
## Purpose
9
+
10
+
This instruction explains when and how to use `Set-ItResult` in Pester tests to mark tests as Pending or Skipped dynamically within test execution.
11
+
12
+
## When to Use Set-ItResult
13
+
14
+
Use `Set-ItResult` when you need to conditionally mark a test as Pending or Skipped based on runtime conditions that can't be determined at test definition time.
15
+
16
+
### Pending vs Skipped
17
+
18
+
**Pending**: Use for tests that should be enabled but temporarily can't run due to:
19
+
- Intermittent external service failures (network, APIs)
20
+
- Known bugs being fixed
21
+
- Missing features being implemented
22
+
- Environmental issues that are being resolved
23
+
24
+
**Skipped**: Use for tests that aren't applicable to the current environment:
25
+
- Platform-specific tests running on wrong platform
26
+
- Tests requiring specific hardware/configuration not present
27
+
- Tests requiring elevated permissions when not available
28
+
- Feature-specific tests when feature is disabled
29
+
30
+
## Pattern
31
+
32
+
### Basic Usage
33
+
34
+
```powershell
35
+
It "Test description" {
36
+
if ($shouldBePending) {
37
+
Set-ItResult -Pending -Because "Explanation of why test is pending"
38
+
return
39
+
}
40
+
41
+
if ($shouldBeSkipped) {
42
+
Set-ItResult -Skipped -Because "Explanation of why test is skipped"
43
+
return
44
+
}
45
+
46
+
# Test code here
47
+
}
48
+
```
49
+
50
+
### Important: Always Return After Set-ItResult
51
+
52
+
After calling `Set-ItResult`, you **must** return from the test to prevent further execution:
53
+
54
+
```powershell
55
+
It "Test that checks environment" {
56
+
if ($env:SKIP_TESTS -eq 'true') {
57
+
Set-ItResult -Skipped -Because "SKIP_TESTS environment variable is set"
58
+
return # This is required!
59
+
}
60
+
61
+
# Test assertions
62
+
$result | Should -Be $expected
63
+
}
64
+
```
65
+
66
+
**Why?** Without `return`, the test continues executing and may fail with errors unrelated to the pending/skipped condition.
67
+
68
+
## Examples from the Codebase
69
+
70
+
### Example 1: Pending for Intermittent Network Issues
71
+
72
+
```powershell
73
+
It "Validate Update-Help for module" {
74
+
if ($markAsPending) {
75
+
Set-ItResult -Pending -Because "Update-Help from the web has intermittent connectivity issues. See issues #2807 and #6541."
76
+
return
77
+
}
78
+
79
+
Update-Help -Module $moduleName -Force
80
+
# validation code...
81
+
}
82
+
```
83
+
84
+
### Example 2: Skipped for Missing Environment
85
+
86
+
```powershell
87
+
It "Test requires CI environment" {
88
+
if (-not $env:CI) {
89
+
Set-ItResult -Skipped -Because "Test requires CI environment to safely install Pester"
90
+
return
91
+
}
92
+
93
+
Install-CIPester -ErrorAction Stop
94
+
}
95
+
```
96
+
97
+
### Example 3: Pending for Platform-Specific Issue
98
+
99
+
```powershell
100
+
It "Clear-Host works correctly" {
101
+
if ($IsARM64) {
102
+
Set-ItResult -Pending -Because "ARM64 runs in non-interactively mode and Clear-Host does not work."
103
+
return
104
+
}
105
+
106
+
& { Clear-Host; 'hi' } | Should -BeExactly 'hi'
107
+
}
108
+
```
109
+
110
+
### Example 4: Skipped for Missing Feature
111
+
112
+
```powershell
113
+
It "Test ACR authentication" {
114
+
if ($env:ACRTESTS -ne 'true') {
115
+
Set-ItResult -Skipped -Because "The tests require the ACRTESTS environment variable to be set to 'true' for ACR authentication."
Copy file name to clipboardExpand all lines: test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1
+33-14Lines changed: 33 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -191,7 +191,8 @@ function RunUpdateHelpTests
191
191
param (
192
192
[string]$tag="CI",
193
193
[switch]$useSourcePath,
194
-
[switch]$userscope
194
+
[switch]$userscope,
195
+
[switch]$markAsPending
195
196
)
196
197
197
198
foreach ($moduleNamein$modulesInBox)
@@ -214,12 +215,17 @@ function RunUpdateHelpTests
214
215
215
216
It ('Validate Update-Help for module ''{0}'' in {1}'-F$moduleName, [PSCustomObject] $updateScope) -Skip:(!(Test-CanWriteToPsHome) -and$userscope-eq$false) {
216
217
218
+
if ($markAsPending) {
219
+
Set-ItResult-Pending -Because "Update-Help from the web has intermittent connectivity issues. See issues #2807 and #6541."
Describe "Validate Update-Help from the Web for all PowerShell modules."-Tags @('Feature','RequireAdminOnWindows','RequireSudoOnUnix') {
@@ -342,7 +361,7 @@ Describe "Validate Update-Help from the Web for all PowerShell modules." -Tags @
342
361
$ProgressPreference=$SavedProgressPreference
343
362
}
344
363
345
-
RunUpdateHelpTests -Tag "Feature"
364
+
RunUpdateHelpTests -Tag "Feature"-MarkAsPending
346
365
}
347
366
348
367
Describe "Validate Update-Help from the Web for all PowerShell modules for user scope."-Tags @('Feature','RequireAdminOnWindows','RequireSudoOnUnix') {
@@ -354,7 +373,7 @@ Describe "Validate Update-Help from the Web for all PowerShell modules for user
0 commit comments