如何对ThrowTerminationError进行Powershell Pester测试

如何对ThrowTerminationError进行Powershell Pester测试,powershell,testing,pester,Powershell,Testing,Pester,如何对ThrowTerminationError进行Powershell Pester测试 catch { $PSCmdlet.ThrowTerminatingError( $PSItem ) } 输出: Missed command: File Class Function Line Command ---- ----- -------- ---- ------- Test-Se

如何对ThrowTerminationError进行Powershell Pester测试

catch
{
    $PSCmdlet.ThrowTerminatingError( $PSItem )
}
输出:

Missed command:

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

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

  • 必须将输入命令括在脚本块中(
    {…}

  • 并测试
    Should-Throw
    /
    Should-not-Throw

注意:如果您也希望以这种方式处理非终止错误,那么
应该-Throw的
不会处理:

  • 如果输入命令调用cmdlet/advanced函数,请向其添加公共参数
    -ErrorAction Stop
    ,这会将(第一个)非终止错误升级为(脚本)-终止(致命)错误

  • 否则,将
    $ErrorActionPreference='Stop'
    设置为脚本块内的第一条语句


示例

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

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

将非终止错误升级为终止错误,并使用
-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
此外,您可以使用
-ErrorId
参数测试特定的错误ID

{ 1 / 0 } | Should -Throw -ErrorId RuntimeException
{ 1 / 0 } | Should -Throw -ExceptionType System.Management.Automation.RuntimeException
由于使用
1/0
触发的终止错误语句(也记录在自动
$error
变量中)生成的错误记录具有
.FullyQualifiedErrorId
RuntimeException
(用
$error[0]验证)。FullyQualifiedErrorId
之后)

注意:
-ErrorId
执行文字子字符串匹配,因此
-ErrorId Runtime
也可以在上面的命令中工作

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

{ 1 / 0 } | Should -Throw -ErrorId RuntimeException
{ 1 / 0 } | Should -Throw -ExceptionType System.Management.Automation.RuntimeException
请注意,必须传递完整的类型名称;省略
系统。
组件(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
  }


}

根据mklement0的回答,您需要在脚本块中封装任何通过管道传输到断言的内容,以便使用
-Throw
检查。这是您需要执行此操作的唯一断言类型

注意:您也可以使用
-Not

Describe 'Test' { 
    It 'Should Throw' { 
        { Throw } | Should -Not -Throw 
    }
}
返回:

Describing Test
  [-] Should Throw 15ms
    Expected no exception to be thrown, but an exception "ScriptHalted" was thrown from line:3 char:11
        +         { Throw } | Should -Not -Throw
        +           ~~~~~.
    3:         { Throw } | Should -Not -Throw