如果你想说,1 应该等于 1,如果不等于 1,那么在 powershell 中使用 pester 避免代码重复最雄辩的方法是什么?
例如
{1 | should be 1} else {break}
而不是
1 | should be 1
if (1 -ne 1) {break}
目前没有用于在发生故障时中断测试执行的内置功能,但已在此处进行了讨论:https://github.com/pester/Pester/issues/360以及一些解决方法,例如这个:
BeforeEach {
$FailedCount = InModuleScope -ModuleName Pester { $Pester.FailedCount }
if ($FailedCount -gt 0) {
Set-ItResult -Skipped -Because 'previous test failed'
}
}
另一种选择可能是将测试分解为几个不同的 Pester 脚本。然后,您可以进行一些高级或初始测试,首先检查它们是否成功,如果它们未全部通过,则跳过其余测试脚本的执行。
例如:
$Failed = (Invoke-Pester -Path First.tests.ps1 -PassThru).FailedCount
If ($Failed -eq 0) {
Invoke-Pester -Path Second.tests.ps1
..
}
现在有内置功能
https://github.com/pester/Pester/pull/2023
$pesterConfig = New-PesterConfiguration
$pesterConfig.Run.Container = @((New-PesterContainer -Path C:\Users\Armaan\Documents\powershell-dev\sample.Tests.ps1), (New-PesterContainer -Path C:\Users\Armaan\Documents\powershell-dev\sample2.Tests.ps1))
$pesterConfig.Output.Verbosity = "Detailed"
$pesterConfig.Run.SkipRemainingOnFailure = 'Run'
Invoke-Pester -Configuration $pesterConfig
我来到这里寻找一种方法来使一个测试依赖于另一个测试。
#Dependency in Pester v3.4.0
Describe 'testing pester' {
$dep = @{}
context 'when in here' {
It 'when the independent test fails' {
$dep.aSpecificDependency = 'failed'
'a' | should be 'b'
$dep.aSpecificDependency = 'passed'
}
It 'then the dependent test is inconclusive' {
if(-not $dep.ContainsKey('aSpecificDependency')){
Set-TestInconclusive -Message 'aSpecificDependency did not run.'
}elseif($dep.aSpecificDependency -ne 'passed'){
Set-TestInconclusive -Message 'aSpecificDependency did not pass.'
}
'a' | should be 'a'
}
It 'it is also inconclusive when the independent has not yet been performed.' {
if(-not $dep.ContainsKey('aDifferentDependency')){
Set-TestInconclusive -Message 'aDifferentDependency did not run.'
}elseif($dep.aDifferentDependency -ne 'passed'){
Set-TestInconclusive -Message 'aDifferentDependency did not pass.'
}
'a' | should be 'a'
}
if($dep.ContainsKey('aSpecificDependency') -and $dep.aSpecificDependency -ne 'passed'){
return
}
It 'stops before running this one' {
'a' | should be 'a'
}
}
context 'when looking at another section' {
It 'goes fine' {
'a' | should be 'a'
}
}
if($dep.ContainsKey('aSpecificDependency') -and $dep.aSpecificDependency -ne 'passed'){
return
}
context 'when looking at another section' {
It 'or you could stop on that too' {
'a' | should be 'a'
}
}
}