简体   繁体   中英

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. I read the option -EnableExit causes Invoke-Pester to return the numer of failed tests. But whenever I use -EnableExit the powershell console closes, regardless if a test failed. It is Pester version 4.7.3. PSVersion 5.1.

Is Invoke-Pester -EnableExit supposed to close the shell?
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 . For example:

$TestResults = Invoke-Pester -PassThru

My $TestResults variable then has a FailedCount property with the number of tests that failed. 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:

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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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