Edit
The crux of the question is: how do I get access to variable(s) declared in a BeforeDiscovery
block in my It
blocks that are not passed through by the it -foreach $var
construct?
I'm having difficulties adjusting to the Discovery/Run phase and Scoping of Variables in Pester 5
Background
We are moving servers and what I'm trying to test is
- that every share on
serverA
also exists onserverB
. - that every readable share on
serverA
is also readable onserverB
.
Using Pester 5, below code runs as intented but to make it work, I have to retrieve the $toShares
twice. Retrieving the shares in my actual tests is using a net view
and is a fairly long running operaton.
- I have to retrieve
$toShares
in the Discovery phase to construct the$readableFromShares
list - I have to retrieve an identical
$toShares
in a BeforeAll block to have them available in theshould exists
test
Question
How can I best restructure my test so that I only need to retrieve the $toShares
once?
Testcode
$PesterPreference = [PesterConfiguration]::Default
$PesterPreference.Output.Verbosity = 'Detailed'
function Test-Path {$True} # hide the real Test-Path function
Describe "Describe" -ForEach @(
@{fromServer ='serverA'; toServer = 'serverB'}
@{fromServer ='serverC'; toServer = 'serverD'}
){
BeforeDiscovery {
$fromShares = 'share1', 'share2', 'share3'
# $toShares is needed here to construct the readableFromShares array
$toShares = 'share1', 'share2'
$readableFromShares = $fromShares |
Where-Object {$toShares -contains $_} |
Where-Object {Test-Path "\\$($fromServer)\$($_)"}
}
Context "<fromServer> samba shares should exist on <toServer>" {
BeforeAll {
# the "same" $toShares is needed here to be available in the exist tests
$toShares = 'share1', 'share2'
}
It "Does \\<toServer>\<_> exist" -ForEach $fromShares {
$toShares -contains $_ | Should -Be $true
}
}
Context "Readable <fromServer> samba shares should als be readable on <toServer>" {
It "<_> is readable on <fromServer>. \\<toServer>\<_> should also be readable." -ForEach $readableFromShares {
Test-Path "\\$($toServer)\$($_)"| Should -Be $True
}
}
}
Output including two deliberate failing tests
Edit
Testcase including
- Two from/to servers (io one)
- Different sharenames for each set of servers
Test
$PesterPreference = [PesterConfiguration]::Default
$PesterPreference.Output.Verbosity = 'Detailed'
function Test-Path {$True} # hides the real Test-Path
function Get-FromShares($fromServer) {if ($fromServer -eq 'serverA') { @('ABshare1', 'ABshare2', 'ABshare3') } else {@('XYshare1', 'XYshare2', 'XYshare3')}}
function Get-ToShares($toServer) {if ($toServer -eq 'serverB') { @('ABshare1', 'ABshare2') } else {@('XYshare1', 'XYshare2')}}
class Shares { static $toShares = @{} }
function Test-Path {$True} # hides the real Test-Path
Describe "Describe" -ForEach @(
@{fromServer ='serverA'; toServer = 'serverB'}
@{fromServer ='serverX'; toServer = 'serverY'}
){
#It "Define shares" -TestCases @( 1 ) { class Shares { static [string[]]$toShares = @('share1', 'share2') } }
BeforeDiscovery {
$fromShares = Get-FromShares($fromServer)
[Shares]::toShares =@{ $fromServer = Get-ToShares($toServer)}
$toShares = [Shares]::toShares[$fromServer]
$readableFromShares = $fromShares |
Where-Object {$toShares -contains $_} |
Where-Object {Test-Path "\\$($fromServer)\$($_)"}
}
Context "<fromServer> samba shares should exist on <toServer>" {
BeforeAll {
$toShares = [Shares]::toShares[$fromServer]
}
It "Does \\<toServer>\<_> exist" -ForEach $fromShares {
$toShares -contains $_ | Should -Be $true
}
}
Context "Readable <fromServer> samba shares should als be readable on <toServer>" {
It "<_> is readable on <fromServer>. \\<toServer>\<_> should also be readable." -ForEach $readableFromShares {
Test-Path "\\$($toServer)\$($_)"| Should -Be $True
}
}
}