Migrating from 4.10 to 5.x and handling -Skip #2284
-
I have a unit test script that looks like I can convert mostly to the new What is the best method to handle this case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, that's a good question!
If that's not possible then you'd have to use
Describe 'Skipcheck in Run' {
It 'first' {
# Run logic to make decision.. To skip or not to skip
$script:ShouldSkipInRun = $true
1 | Should -Be 1
}
It 'second' {
if ($script:ShouldSkipInRun) {
Set-ItResult -Skipped -Because 'first test said so'
}
1 | Should -Be 1
}
} |
Beta Was this translation helpful? Give feedback.
Hi, that's a good question!
-Skip
is evaluated as part of Discovery (see https://pester.dev/docs/usage/skip#conditional-skip). Ideally you'd run some code inBeforeDiscovery
to figure out if you need them or not.If that's not possible then you'd have to use
Set-ItResult
to skip. You will have to use a script-scoped (or worst case global) variable to share the variable betweenIt
-blocks.$script:
-scope is normally per*.tests.ps1
-file unless you run code inside a module function or something likeInModuleScope SomeModule { .. }
. Remember to use unique variable names and/or clear it when it shouldn't apply anymore, ex. in aAfterAll
. Especially with global-variable that would affect the ne…