如果Pester断言失败,该如何指定该怎么办?

时间:2020-08-05 13:28:51

标签: powershell pester

如果您想说1应该等于1,并且如果它没有那么破损,那么在powershell中避免使用代码重复的最有说服力的方法是什么?

例如

{1 | should be 1} else {break}

而不是

1 | should be 1
if (1 -ne 1) {break}

1 个答案:

答案 0 :(得分:4)

目前没有发生故障时中断测试执行的内置功能,但已在此处进行了讨论: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
    ..
}
相关问题