gpt4 book ai didi

powershell - 我如何通过 Powershell Pester 测试 ThrowTerminatingError

转载 作者:行者123 更新时间:2023-11-28 19:56:51 25 4
gpt4 key购买 nike

我如何通过 Powershell Pester 测试 ThrowTerminatingError?

catch
{
$PSCmdlet.ThrowTerminatingError( $PSItem )
}

输出:

Missed command:

File Class Function Line Command
---- ----- -------- ---- -------
Test-ServerOnline.ps1 Test-ServerOnline 50 $PSCmdlet.ThrowTerminatingError( $PSItem )

最佳答案

为了在 Pester 测试中捕获终止错误(异常):

  • 您必须将输入命令包含在脚本 block ( { ... } )

  • 并测试
    Should -Throw 是否出现/未出现此类错误/Should -Not -Throw

注意:如果您也想以这种方式处理非终止错误Should -Throw 处理:

  • 如果输入命令调用cmdlet/高级函数,添加常用参数-ErrorAction Stop到它,这将(第一个)非终止错误提升为(脚本)终止(致命)错误。

  • 否则,设置$ErrorActionPreference = 'Stop'作为第一个语句在脚本 block 中


示例:

注意:为简洁起见,以下片段不是完整的 Pester 测试,仅调用 Pester Should命令;但是,您可以按原样调用它们,在这种情况下,没有输出 就意味着测试成功。有关完整示例,请参阅底部部分。

# Note the { ... } around the command.
{ 1 / 0 } | Should -Throw

上面的测试通过了,因为1 / 0创建一个语句终止错误,这是与 $PSCmdlet.ThrowTerminatingError() 相同的错误类型产生。

非终止错误升级为终止错误 -ErrorAction Stop

# Note: Without -ErrorAction Stop, the test would fail, because
# not finding a file is a non-terminating error.
{ Get-Item NoSuchFile -ErrorAction Stop } | Should -Throw

此外,您可以测试特定的错误ID,使用 -ErrorId 参数:

{ 1 / 0 } | Should -Throw -ErrorId RuntimeException

上面的测试通过了,因为用1 / 0触发的语句终止错误产生的错误记录(也记录在自动 $Error 变量中),有一个 .FullyQualifiedErrorId RuntimeException 的值(之后用 $Error[0].FullyQualifiedErrorId 验证)。

注意: -ErrorId执行文字子串匹配,因此 -ErrorId Runtime在上面的命令中也会起作用。

或者,您可以测试特定异常类型,使用 -ExceptionType 参数:

{ 1 / 0 } | Should -Throw -ExceptionType System.Management.Automation.RuntimeException

请注意,您必须传递完整类型名称;省略 System.组件(PowerShell 通常允许)-ExceptionType 识别.

要识别与最近错误关联的异常类型,请使用 $Error[0].Exception.GetType().FullName


完整示例:

将以下内容存储在 *.tests.ps1 中文件并直接或通过 Invoke-Pester 调用它.
所有这些测试都应该通过。

Describe "Error-handling tests" {

BeforeAll {
# Define an advanced function that generates a terminating error.
function Get-FooTerminating {
[CmdletBinding()]
param()
# When invoked by Pester, $PSCmdlet.ThrowTerminatingError()
# would have the same effect here, but note that $PSCmdlet.ThrowTerminatingError()
# creates a *statement*-terminating error, whereas Throw creates a more
# severe *script*-terminating (thread-terminating) error.
Throw "me for a loop"
}

# Define an advanced function that generates a non-terminating error.
function Get-FooNonTerminating {
[CmdletBinding()]
param()
Write-Error "Something went mildly wrong."
}

}

It "A terminating error throws." {
{ Get-FooTerminating } | Should -Throw
}

It "A non-terminating error doesn't throw." {
# Note: Non-terminating errors are *passed through*, so
# we silence them with 2>$null here.
{ Get-FooNonTerminating 2>$null } | Should -Not -Throw
}

It "A non-terminating error promoted to a terminating one does throw." {
{ Get-FooNonTerminating -ErrorAction Stop } | Should -Throw
}

It "A fully qualified error ID can be tested for." {
# Test for a (substring of) the error record's .FullyQualifiedErrorId
{ NoSuchCommand } | Should -Throw -ErrorId CommandNotFound
}

It "A specific exception type can be tested for." {
# Note the need to specify the type name *in full*, including the
# 'System.' part
{ NoSuchCommand } | Should -Throw -ExceptionType System.Management.Automation.CommandNotFoundException
}


}

关于powershell - 我如何通过 Powershell Pester 测试 ThrowTerminatingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com