简体   繁体   English

如何从Invoke-Pester获取失败测试的数目?

[英]How can I get the numer of failed tests from Invoke-Pester?

I have a few Pester tests running fine in the console, but I would like to run the tests automatically and send a message if any test fails. 我的控制台中有一些Pester测试运行良好,但是我想自动运行测试,如果任何测试失败,则发送一条消息。 I read the option -EnableExit causes Invoke-Pester to return the numer of failed tests. 我阅读了-EnableExit选项,导致Invoke-Pester返回失败测试的数目。 But whenever I use -EnableExit the powershell console closes, regardless if a test failed. 但是无论何时我使用-EnableExit,PowerShell控制台都会关闭,无论测试是否失败。 It is Pester version 4.7.3. 它是Pester版本4.7.3。 PSVersion 5.1. PSVersion 5.1。

Is Invoke-Pester -EnableExit supposed to close the shell? Invoke-Pester -EnableExit应该关闭外壳?
How do I get the number of failed tests? 如何获得失败的测试数量?

runs fine: 运行良好:
Invoke-Pester -Script D:\\tmp\\PowerShell\\dummy1.Tests.ps1

closes the shell window: 关闭外壳窗口:
Invoke-Pester -Script D:\\tmp\\PowerShell\\dummy1.Tests.ps1 -EnableExit

I expect to get an integer as output, but the console window closes. 我希望得到一个整数作为输出,但是控制台窗口关闭。

You can get the number of failed tests by using the -PassThru switch on Invoke-Pester . 您可以使用Invoke-Pester上的-PassThru开关来获取失败测试的次数。 For example: 例如:

$TestResults = Invoke-Pester -PassThru

My $TestResults variable then has a FailedCount property with the number of tests that failed. 然后,我的$TestResults变量具有FailedCount属性,其中包含失败的测试数量。 You can then use this as part of a pipeline to have the pipeline fail if there are failed tests: 然后,如果测试失败,则可以将其用作管道的一部分以使管道失败:

If ($TestResults.FailedCount -gt 0) { Throw "There were $($TestResults.FailedCount) failed tests" }

Here's an example of the other things -PassThru returns: 这是-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=}...}

Use the -PassThru switch parameter of Invoke-Pester 使用Invoke-Pester-PassThru开关参数

$Result = Invoke-Pester -Script C:\temp\test.tests.ps1 -PassThru
$Result
$Result.FailedCount

You can get just the number of failed tests by doing this: 通过执行以下操作,您可以得到失败测试的数量:

(Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None).FailedCount

If you want other data (passed/skipped count, test results, etc), then pass the output to a variable, then process further: 如果您需要其他数据(通过/跳过计数,测试结果等),则将输出传递给变量,然后进一步处理:

$testResults = Invoke-Pester -Path D:\\tmp\\PowerShell\\dummy1.Tests.ps1 -PassThru -Show None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM