佩斯特5.6.1 Powershell 7.4.5
这就是交易。 我正在 BeforeDiscovery 中加载数据。 我可以在运行阶段引用所有变量...没问题。 甚至字符串数组。 我不能做的就是循环字符串数组。 它只是完全忽略它们:
在
$account
中定义 BeforeDiscovery
是不够的,与 -ForEach
使用的所有变量都必须在发现阶段可用。
在
$roles
内的 BeforeDiscovery
块中定义 Describe
。例如
$c = New-PesterConfiguration
$c.Run.ScriptBlock = {
BeforeDiscovery {
$account = @{
'name' = 'one'
'roles' = @('web', 'db')
}, @{
'name' = 'two'
'roles' = @('app', 'file')
}
}
Describe 'Account <name>' -ForEach $account {
BeforeDiscovery {
[string[]]$serverroles = $_.roles
}
Context 'Has role <_>' -ForEach $serverroles {
It 'Test 1' -T {
1 | Should -Be 1
}
}
}
}
$c.Output.Verbosity = 'Detailed'
$c.Run.PassThru = $true
$r = Invoke-Pester -Configuration $c
# Output
Starting discovery in 1 files.
Discovery found 4 tests in 26ms.
Running tests.
Describing Account one
Context Has role web
[+] Test 1 6ms (1ms|4ms)
Context Has role db
[+] Test 1 3ms (1ms|1ms)
Describing Account two
Context Has role app
[+] Test 1 3ms (1ms|2ms)
Context Has role file
[+] Test 1 2ms (1ms|1ms)
Tests completed in 153ms
Tests Passed: 4, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0