Pester没有抓住抛出的错误

时间:2018-03-08 19:58:40

标签: powershell pester

当我运行以下pester测试时,我希望它能够捕获预期的错误,但它并没有。但是,当我使用不同的函数运行测试时,它使用不同的throw语句。

Pester测试:

Describe "Remove-GenericCredential Function Tests" {
  $InternetOrNetworkAddress = 'https://PesterTestUser@PesterTestURl.com'

  Context "Test:  Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" {
  It "This Command threw an error.  The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" }
  }
}

未发现错误:

Remove-GenericCredential : Credential https://PesterTestUser@PesterTestURl.com not found

At C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1:30 char:76
+ ... xist." { { (Remove-GenericCredential -InternetOrNetworkAddress $Inter ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-GenericCredential

      [-] This Command threw an error.  The credential does not exist. 44ms
        Expected: the expression to throw an exception with message {Remove-GenericCredential : Credential https://PesterTestUser@PesterTestURl.com not found}, an exception was not raised, message was {}
            from C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\New-GitHubCredential.Tests.ps1:59 char:176
            + ... e $UserName -Token 'NotAGitHubTokenSpecialCharacters!@#$%^&*') } | sh ...
            +                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        at <ScriptBlock>, C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1: line 30
        30:     It "This Command threw an error.  The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw 'Remove-GenericCredential : Credential https://PesterTestUser@PesterTestURl.com not found' }

2 个答案:

答案 0 :(得分:1)

这可能是因为cmdlet抛出了非终止错误,因为Pester只会断言终止错误。在这种情况下需要重写测试如下(根据您的示例使用旧的Pester v3 synatax):

It "Test a non-terminating error gets thrown" {
  $errorThrown = $false;
  try
  {
    Invoke-CmdletThatThrowsNonTerminatingError -ErrorAction Stop
  }
  catch
  {
    $errorThrown = $true
  }
  $errorThrown | Should Be $true
}

在catch块中,您还可以在$_ ob

上获取异常详细信息,例如消息或异常类型

答案 1 :(得分:0)

根据另一个答案,该函数抛出一个non-terminating error,因此它不被认为与Should Throw的测试相匹配,Write-Error正在检查终止错误。

有两种方法可以解决这个问题:

  1. 您可以更改它,以便通过将Throw更改为-ErrorAction Stop来引发终止错误。
  2. 您可以更改测试以强制该函数抛出终止错误,即使在您调用它时使用-Confirm发生非终止错误(我可以看到您正在使用[cmdletbinding()] ,我假设您已在函数中使用-ErrorAction来添加常见参数,如Function Remove-GenericCredential { [cmdletbinding(supportsshouldprocess)] Param( $InternetOrNetworkAddress ) Write-Error "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" } Describe "Remove-GenericCredential Function Tests" { $InternetOrNetworkAddress = 'https://PesterTestUser@PesterTestURl.com' Context "Test: Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" { It "This Command threw an error. The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false -ErrorAction Stop) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" } } } )。
  3. 以下是第二个解决方案的示例(我已经模拟了顶部的功能,以便我可以对此进行测试,但您不需要在测试脚本中包含该功能):

    help Write-Error

    来自import pandas as pd import numpy as np data = pd.DataFrame({"Name":["Bob", "Mary"], "Age":[50,55],"ID1":["1,2",np.NaN],"ID2":["1,3","4,5,6"]}) Age ID1 ID2 Name 0 50 1,2 1,3 Bob 1 55 NaN 4,5,6 Mary

      

    Write-Error cmdlet声明了一个非终止错误。默认情况下,   错误在错误流中发送到主机程序   显示,以及输出。

         

    要编写非终止错误,请输入错误消息字符串,ErrorRecord对象或Exception对象。使用Write-Error的其他参数填充错误记录。

         

    非终止错误会向错误流写入错误,但它们不会停止命令处理。如果在输入项集合中的一个项目上声明了非终止错误,则该命令将继续处理集合中的其他项目。

         

    要声明终止错误,请使用Throw关键字。有关更多信息,请参阅about_Throw   (http://go.microsoft.com/fwlink/?LinkID=145153)。