Skip to content

Commit d324f29

Browse files
fflatennohwnd
andauthored
Skip blocks without runnable tests (#2447)
* Use new certificate thumbprint * Skip blocks when no tests should execute * Don't modify blocks that are excluded Caused wrong Result as Skipped takes precedence over NotRun * Add test for onetime setup/teardown on skipped blocks * Update test * Cleanup --------- Co-authored-by: Jakub Jareš <[email protected]>
1 parent 93c6610 commit d324f29

File tree

2 files changed

+125
-45
lines changed

2 files changed

+125
-45
lines changed

src/Pester.Runtime.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,7 @@ function PostProcess-DiscoveredBlock {
20732073
}
20742074

20752075
$blockShouldRun = $false
2076+
$allTestsSkipped = $true
20762077
if ($tests.Count -gt 0) {
20772078
foreach ($t in $tests) {
20782079
$t.Block = $b
@@ -2152,11 +2153,19 @@ function PostProcess-DiscoveredBlock {
21522153
$testsToRun[-1].Last = $true
21532154
$blockShouldRun = $true
21542155
}
2156+
2157+
foreach ($t in $testsToRun) {
2158+
if (-not $t.Skip) {
2159+
$allTestsSkipped = $false
2160+
break
2161+
}
2162+
}
21552163
}
21562164
}
21572165

21582166
$childBlocks = $b.Blocks
21592167
$anyChildBlockShouldRun = $false
2168+
$allChildBlockSkipped = $true
21602169
if ($childBlocks.Count -gt 0) {
21612170
foreach ($cb in $childBlocks) {
21622171
$cb.Parent = $b
@@ -2171,9 +2180,17 @@ function PostProcess-DiscoveredBlock {
21712180
$childBlocksToRun[0].First = $true
21722181
$childBlocksToRun[-1].Last = $true
21732182
}
2183+
2184+
foreach ($cb in $childBlocksToRun) {
2185+
if (-not $cb.Skip) {
2186+
$allChildBlockSkipped = $false
2187+
break
2188+
}
2189+
}
21742190
}
21752191

21762192
$shouldRunBasedOnChildren = $blockShouldRun -or $anyChildBlockShouldRun
2193+
$shouldSkipBasedOnChildren = $allTestsSkipped -and $allChildBlockSkipped
21772194

21782195
if ($b.ShouldRun -and -not $shouldRunBasedOnChildren) {
21792196
if ($PesterPreference.Debug.WriteDebugMessages.Value) {
@@ -2182,6 +2199,26 @@ function PostProcess-DiscoveredBlock {
21822199
}
21832200

21842201
$b.ShouldRun = $shouldRunBasedOnChildren
2202+
2203+
if ($b.ShouldRun) {
2204+
if (-not $b.Skip -and $shouldSkipBasedOnChildren) {
2205+
if ($PesterPreference.Debug.WriteDebugMessages.Value) {
2206+
if ($b.IsRoot) {
2207+
Write-PesterDebugMessage -Scope Skip "($($b.BlockContainer)) Container will be skipped because all included children are marked as skipped."
2208+
} else {
2209+
Write-PesterDebugMessage -Scope Skip "($($b.Path -join '.')) Block will be skipped because all included children are marked as skipped."
2210+
}
2211+
}
2212+
$b.Skip = $true
2213+
} elseif ($b.Skip -and -not $shouldSkipBasedOnChildren) {
2214+
if ($PesterPreference.Debug.WriteDebugMessages.Value) {
2215+
Write-PesterDebugMessage -Scope Skip "($($b.Path -join '.')) Block was marked as skipped, but one or more children are explicitly requested to be run, so the block itself will not be skipped."
2216+
}
2217+
# This is done to execute setup and teardown before explicitly included tests, e.g. using line filter
2218+
# Remaining children have already inherited block-level Skip earlier in this function as expected
2219+
$b.Skip = $false
2220+
}
2221+
}
21852222
}
21862223
}
21872224

tst/Pester.Runtime.ts.ps1

Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,52 +1225,49 @@ i -PassThru:$PassThru {
12251225
$container.EachTestTeardown | Verify-Equal 0
12261226
}
12271227

1228-
t "skipping all items in a block will skip the parent block" {
1229-
# this is not implemented, but is a possible feature
1230-
# which could be implemented together with "if any test is explicitly unskipped in child
1231-
# then the block should run, this will be needed for running tests explicitly by path I think
1232-
# it also should be taken into consideration whether or not adding a lazySkip is a good idea and how it would
1233-
# affect implementation of this. Right now skipping the block goes from parent down, and skipping all items in a block
1234-
# will not prevent the parent block setups from running
1235-
1236-
# $container = @{
1237-
# OneTimeTestSetup = 0
1238-
# OneTimeTestTeardown = 0
1239-
# EachTestSetup = 0
1240-
# EachTestTeardown = 0
1241-
# TestRun = 0
1242-
# }
1228+
t 'skipping all items in a block will skip the parent block' {
1229+
$container = @{
1230+
OneTimeTestSetup = 0
1231+
OneTimeTestTeardown = 0
1232+
EachTestSetup = 0
1233+
EachTestTeardown = 0
1234+
TestRun = 0
1235+
}
1236+
1237+
$actual = Invoke-Test -SessionState $ExecutionContext.SessionState -BlockContainer (New-BlockContainerObject -ScriptBlock {
1238+
New-OneTimeTestSetup -ScriptBlock { $container.OneTimeTestSetup++ }
1239+
New-OneTimeTestTeardown -ScriptBlock { $container.OneTimeTestTeardown++ }
1240+
1241+
New-Block 'parent block' {
1242+
New-OneTimeTestSetup -ScriptBlock { $container.OneTimeTestSetup++ }
1243+
New-OneTimeTestTeardown -ScriptBlock { $container.OneTimeTestTeardown++ }
1244+
1245+
New-EachTestSetup -ScriptBlock { $container.EachTestSetup++ }
1246+
New-EachTestTeardown -ScriptBlock { $container.EachTestTeardown++ }
1247+
1248+
New-Test 'test1' -Skip {
1249+
$container.TestRun++
1250+
'a'
1251+
}
12431252

1244-
# $actual = Invoke-Test -SessionState $ExecutionContext.SessionState -BlockContainer (New-BlockContainerObject -ScriptBlock {
1245-
# New-Block "parent block" {
1246-
# New-Block "parent block" {
1247-
# # putting this in child block because each test setup is not supported in root block
1248-
# New-OneTimeTestSetup -ScriptBlock { $container.OneTimeTestSetup++ }
1249-
# New-OneTimeTestTeardown -ScriptBlock { $container.OneTimeTestTeardown++ }
1250-
1251-
# New-EachTestSetup -ScriptBlock { $container.EachTestSetup++ }
1252-
# New-EachTestTeardown -ScriptBlock { $container.EachTestTeardown++ }
1253-
1254-
# New-Test "test1" -Skip {
1255-
# $container.TestRun++
1256-
# "a"
1257-
# }
1258-
1259-
# New-Test "test2" -Skip {
1260-
# $container.TestRun++
1261-
# "a"
1262-
# }
1263-
# }
1264-
# }
1265-
# })
1266-
1267-
# # $actual.Blocks[0].Skip | Verify-True
1268-
# $actual.Blocks[0].ErrorRecord.Count | Verify-Equal 0
1269-
# $container.TestRun | Verify-Equal 0
1270-
# $container.OneTimeTestSetup | Verify-Equal 0
1271-
# $container.OneTimeTestTeardown | Verify-Equal 0
1272-
# $container.EachTestSetup | Verify-Equal 0
1273-
# $container.EachTestTeardown | Verify-Equal 0
1253+
New-Block 'inner block' -Skip {
1254+
New-Test 'test2' {
1255+
$container.TestRun++
1256+
'a'
1257+
}
1258+
}
1259+
}
1260+
})
1261+
1262+
# Should be marked as Skip by runtime
1263+
$actual.Blocks[0].Skip | Verify-True
1264+
$actual.Blocks[0].ErrorRecord.Count | Verify-Equal 0
1265+
1266+
$container.TestRun | Verify-Equal 0
1267+
$container.OneTimeTestSetup | Verify-Equal 0
1268+
$container.OneTimeTestTeardown | Verify-Equal 0
1269+
$container.EachTestSetup | Verify-Equal 0
1270+
$container.EachTestTeardown | Verify-Equal 0
12741271
}
12751272
}
12761273

@@ -1341,6 +1338,52 @@ i -PassThru:$PassThru {
13411338
$container.EachBlockTeardown1 | Verify-Equal 1
13421339
# $container.OneTimeBlockTeardown1 | Verify-Equal 1
13431340
}
1341+
1342+
t 'setup and teardown are executed on skipped parent blocks when a test is explicitly included' {
1343+
$container = @{
1344+
OneTimeTestSetup = 0
1345+
OneTimeTestTeardown = 0
1346+
EachTestSetup = 0
1347+
EachTestTeardown = 0
1348+
TestRun = 0
1349+
}
1350+
1351+
$sb = {
1352+
New-OneTimeTestSetup -ScriptBlock { $container.OneTimeTestSetup++ }
1353+
New-OneTimeTestTeardown -ScriptBlock { $container.OneTimeTestTeardown++ }
1354+
1355+
New-Block 'parent block' -Skip {
1356+
New-OneTimeTestSetup -ScriptBlock { $container.OneTimeTestSetup++ }
1357+
New-OneTimeTestTeardown -ScriptBlock { $container.OneTimeTestTeardown++ }
1358+
1359+
New-EachTestSetup -ScriptBlock { $container.EachTestSetup++ }
1360+
New-EachTestTeardown -ScriptBlock { $container.EachTestTeardown++ }
1361+
1362+
New-Test 'test1' -Skip { # <--- Linefilter here ($sb assignment + 11 lines). Should run
1363+
$container.TestRun++
1364+
'a'
1365+
}
1366+
1367+
New-Test 'test2' -Skip { # Should not run
1368+
$container.TestRun++
1369+
'a'
1370+
}
1371+
}
1372+
}
1373+
1374+
$f = New-FilterObject -Line "$($sb.File):$($sb.StartPosition.StartLine + 11)"
1375+
$actual = Invoke-Test -SessionState $ExecutionContext.SessionState -BlockContainer (New-BlockContainerObject -ScriptBlock $sb) -Filter $f
1376+
1377+
# Should be marked as Skip = false by runtime
1378+
$actual.Blocks[0].Skip | Verify-False
1379+
$actual.Blocks[0].ErrorRecord.Count | Verify-Equal 0
1380+
1381+
$container.TestRun | Verify-Equal 1
1382+
$container.OneTimeTestSetup | Verify-Equal 2
1383+
$container.OneTimeTestTeardown | Verify-Equal 2
1384+
$container.EachTestSetup | Verify-Equal 1
1385+
$container.EachTestTeardown | Verify-Equal 1
1386+
}
13441387
}
13451388

13461389
b "plugins" {

0 commit comments

Comments
 (0)