[英]How can I get the numer of failed tests from Invoke-Pester?
我的控制台中有一些Pester測試運行良好,但是我想自動運行測試,如果任何測試失敗,則發送一條消息。 我閱讀了-EnableExit選項,導致Invoke-Pester返回失敗測試的數目。 但是無論何時我使用-EnableExit,PowerShell控制台都會關閉,無論測試是否失敗。 它是Pester版本4.7.3。 PSVersion 5.1。
Invoke-Pester -EnableExit
應該關閉外殼?
如何獲得失敗的測試數量?
運行良好:
Invoke-Pester -Script D:\\tmp\\PowerShell\\dummy1.Tests.ps1
關閉外殼窗口:
Invoke-Pester -Script D:\\tmp\\PowerShell\\dummy1.Tests.ps1 -EnableExit
我希望得到一個整數作為輸出,但是控制台窗口關閉。
您可以使用Invoke-Pester
上的-PassThru
開關來獲取失敗測試的次數。 例如:
$TestResults = Invoke-Pester -PassThru
然后,我的$TestResults
變量具有FailedCount
屬性,其中包含失敗的測試數量。 然后,如果測試失敗,則可以將其用作管道的一部分以使管道失敗:
If ($TestResults.FailedCount -gt 0) { Throw "There were $($TestResults.FailedCount) failed tests" }
這是-PassThru
返回的其他示例:
TagFilter :
ExcludeTagFilter :
TestNameFilter :
ScriptBlockFilter :
TotalCount : 230
PassedCount : 229
FailedCount : 1
SkippedCount : 0
PendingCount : 0
InconclusiveCount : 0
Time : 00:00:43.8675480
TestResult : {@{ErrorRecord=; ParameterizedSuiteName=; Describe=Testing all Modules against default
PSScriptAnalyzer rule-set; Parameters=System.Collections.Specialized.OrderedDictionary;
Passed=True; Show=All; FailureMessage=; Time=00:00:00.7463377; Name=passes the PSScriptAnalyzer
Rule PSAlignAssignmentStatement; Result=Passed; Context=Testing Module
'C:\Users\wragg\github\PowerShell-Subnet\Subnet\Subnet.psm1'; StackTrace=}, @{ErrorRecord=;
ParameterizedSuiteName=; Describe=Testing all Modules against default PSScriptAnalyzer rule-set;
Parameters=System.Collections.Specialized.OrderedDictionary; Passed=True; Show=All;
FailureMessage=; Time=00:00:02.2605400; Name=passes the PSScriptAnalyzer Rule
PSAvoidUsingCmdletAliases; Result=Passed; Context=Testing Module
'C:\Users\wragg\github\PowerShell-Subnet\Subnet\Subnet.psm1'; StackTrace=}, @{ErrorRecord=;
ParameterizedSuiteName=; Describe=Testing all Modules against default PSScriptAnalyzer rule-set;
Parameters=System.Collections.Specialized.OrderedDictionary; Passed=True; Show=All;
FailureMessage=; Time=00:00:00.0865224; Name=passes the PSScriptAnalyzer Rule
PSAvoidAssignmentToAutomaticVariable; Result=Passed; Context=Testing Module
'C:\Users\wragg\github\PowerShell-Subnet\Subnet\Subnet.psm1'; StackTrace=}, @{ErrorRecord=;
ParameterizedSuiteName=; Describe=Testing all Modules against default PSScriptAnalyzer rule-set;
Parameters=System.Collections.Specialized.OrderedDictionary; Passed=True; Show=All;
FailureMessage=; Time=00:00:00.0590095; Name=passes the PSScriptAnalyzer Rule
PSAvoidDefaultValueSwitchParameter; Result=Passed; Context=Testing Module
'C:\Users\wragg\github\PowerShell-Subnet\Subnet\Subnet.psm1'; StackTrace=}...}
使用Invoke-Pester
的-PassThru
開關參數
$Result = Invoke-Pester -Script C:\temp\test.tests.ps1 -PassThru
$Result
$Result.FailedCount
通過執行以下操作,您可以得到失敗測試的數量:
(Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None).FailedCount
如果您需要其他數據(通過/跳過計數,測試結果等),則將輸出傳遞給變量,然后進一步處理:
$testResults = Invoke-Pester -Path D:\\tmp\\PowerShell\\dummy1.Tests.ps1 -PassThru -Show None
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.