@@ -133,11 +133,21 @@ function ConvertTo-MtMaesterResult {
133133 $severity = $testSetting.Severity
134134 }
135135
136- # Setting Result to Error, Overwriting the Skipped state
137- if ($testResultDetail.TestSkipped -eq " Error" ) {
138- $result = " Error"
139- } else {
140- $result = $test.Result
136+ # Setting Result to Error, Overwriting the Skipped state
137+ if ($testResultDetail.TestSkipped -eq " Error" ) {
138+ $result = " Error"
139+ } elseif ((
140+ $test -and
141+ $test.ErrorRecord -and
142+ $test.ErrorRecord.Count -gt 0 -and
143+ $test.ErrorRecord [0 ].CategoryInfo -and
144+ $test.ErrorRecord [0 ].CategoryInfo.Reason) -and
145+ (@ (" RuntimeException" , " ParameterBindingValidationException" , " HttpRequestException" , " TaskCanceledException" ) -Contains $test.ErrorRecord [0 ].CategoryInfo.Reason )) {
146+ Write-Verbose " Setting result=Error $ ( $name ) because: $ ( $test.ErrorRecord [0 ].CategoryInfo.Reason) "
147+ $result = " Error"
148+ }
149+ else {
150+ $result = $test.Result
141151 }
142152
143153 $timeSpanFormat = ' hh\:mm\:ss'
@@ -147,7 +157,7 @@ function ConvertTo-MtMaesterResult {
147157 Title = $testTitle
148158 Name = $name
149159 HelpUrl = $helpUrl
150- Severity = $severity
160+ Severity = $severity
151161 Tag = @ ($test.Block.Tag + $test.Tag | Select-Object - Unique)
152162 Result = $result
153163 ScriptBlock = $test.ScriptBlock.ToString ()
@@ -159,41 +169,57 @@ function ConvertTo-MtMaesterResult {
159169 }
160170 $mtTests += $mtTestInfo
161171 }
172+ # Count all Passed, Failed, Skipped, Error, NotRun and Total results
173+ $Recount = [PSCustomObject ]@ {
174+ FailedCount = 0
175+ PassedCount = 0
176+ SkippedCount = 0
177+ NotRunCount = 0
178+ ErrorCount = 0
179+ TotalCount = 0
180+ }
181+ $Recount.FailedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Failed' }).Count
182+ $Recount.PassedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Passed' }).Count
183+ $Recount.ErrorCount = @ ($mtTests | Where-Object { $_.Result -eq ' Error' }).Count
184+ $Recount.SkippedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Skipped' }).Count
185+ $Recount.NotRunCount = @ ($mtTests | Where-Object { $_.Result -eq ' NotRun' }).Count
186+ $Recount.TotalCount = $mtTests.Count
187+ Write-Verbose " Recount: $ ( $Recount | Out-String ) "
162188
163189 $mtBlocks = @ ()
164190 foreach ($container in $PesterResults.Containers ) {
165191
166192 foreach ($block in $container.Blocks ) {
167193 $mtBlockInfo = $mtBlocks | Where-Object { $_.Name -eq $block.Name }
168194 if ($null -eq $mtBlockInfo ) {
195+ Write-Verbose " Recalculating block: $ ( $block.Name ) "
169196 $mtBlockInfo = [PSCustomObject ]@ {
170197 Name = $block.Name
171198 Result = $block.Result
172- FailedCount = $block.FailedCount
173- PassedCount = $block.PassedCount
174- SkippedCount = $block.SkippedCount
175- NotRunCount = $block.NotRunCount
176- TotalCount = $block.TotalCount
199+ FailedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Failed' -and $_.Block -eq $block.name }).Count
200+ PassedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Passed' -and $_.Block -eq $block.name }).Count
201+ ErrorCount = @ ($mtTests | Where-Object { $_.Result -eq ' Error' -and $_.Block -eq $block.name }).Count
202+ SkippedCount = @ ($mtTests | Where-Object { $_.Result -eq ' Skipped' -and $_.Block -eq $block.name }).Count
203+ NotRunCount = @ ($mtTests | Where-Object { $_.Result -eq ' NotRun' -and $_.Block -eq $block.name }).Count
204+ TotalCount = @ ($mtTests | Where-Object { $_.Block -eq $block.name }).Count
177205 Tag = $block.Tag
178206 }
179207 $mtBlocks += $mtBlockInfo
180- } else {
181- $mtBlockInfo.FailedCount += $block.FailedCount
182- $mtBlockInfo.PassedCount += $block.PassedCount
183- $mtBlockInfo.SkippedCount += $block.SkippedCount
184- $mtBlockInfo.NotRunCount += $block.NotRunCount
185- $mtBlockInfo.TotalCount += $block.TotalCount
208+ }
209+ else {
210+ # We already seen and counted all blocks
186211 }
187212 }
188213 }
189214
190215 $mtTestResults = [PSCustomObject ]@ {
191216 Result = $PesterResults.Result
192- FailedCount = $PesterResults.FailedCount
193- PassedCount = $PesterResults.PassedCount
194- SkippedCount = $PesterResults.SkippedCount
195- NotRunCount = $PesterResults.NotRunCount
196- TotalCount = $PesterResults.TotalCount
217+ FailedCount = $Recount.FailedCount
218+ PassedCount = $Recount.PassedCount
219+ ErrorCount = $Recount.ErrorCount
220+ SkippedCount = $Recount.SkippedCount
221+ NotRunCount = $Recount.NotRunCount
222+ TotalCount = $Recount.TotalCount
197223 ExecutedAt = GetFormattedDate($PesterResults.ExecutedAt )
198224 TotalDuration = $PesterResults.Duration.ToString ($timeSpanFormat )
199225 UserDuration = $PesterResults.UserDuration.ToString ($timeSpanFormat )
0 commit comments