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=}...}