Skip to content

Commit d9fbbed

Browse files
🪲 [Fix]: Improve test results failure handling and reporting (#6)
The action now provides more detailed and actionable error messages when test results fail. When tests don't execute properly or result files are missing, you'll receive specific information about what went wrong, making it easier to diagnose and fix issues with your test suites. - Fixes PSModule/Process-PSModule#223 ## Improved Error Reporting The action now generates specific error messages for each type of test failure, including: - Missing result files - Tests that weren't executed - Failed test results - No tests reported - No tests passed - Inconclusive tests - Tests that weren't run Each error message clearly identifies which test suite and result file is affected, helping you quickly locate and resolve issues. ## Safer Property Access The action now checks for null values before accessing test result properties (Tests, Passed, Failed, Inconclusive, NotRun). This prevents errors when processing incomplete or malformed test result files. ## Better Categorization Missing result files are now tracked separately from unexecuted tests, providing clearer insight into whether a test file wasn't found in the artifacts or if it was present but marked as not executed.
1 parent 13b6dbd commit d9fbbed

1 file changed

Lines changed: 62 additions & 12 deletions

File tree

scripts/main.ps1

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ LogGroup 'Expected test suites' {
7575
$isFailure = $false
7676

7777
$testResults = [System.Collections.Generic.List[psobject]]::new()
78-
$failedTests = [System.Collections.Generic.List[psobject]]::new()
79-
$unexecutedTests = [System.Collections.Generic.List[psobject]]::new()
78+
$failedTests = [System.Collections.Generic.List[string]]::new()
79+
$unexecutedTests = [System.Collections.Generic.List[string]]::new()
80+
$missingResultFiles = [System.Collections.Generic.List[string]]::new()
8081
$totalErrors = 0
8182

8283
foreach ($expected in $expectedTestSuites) {
@@ -109,14 +110,21 @@ foreach ($expected in $expectedTestSuites) {
109110
}
110111

111112
# Determine if there’s any failure for this single test file
113+
$hasTestsValue = $null -ne $result.Tests
114+
$hasPassedValue = $null -ne $result.Passed
115+
$hasFailedValue = $null -ne $result.Failed
116+
$hasInconclusiveValue = $null -ne $result.Inconclusive
117+
$hasNotRunValue = $null -ne $result.NotRun
118+
112119
$testFailure = (
113120
$result.Result -ne 'Passed' -or
114121
$result.Executed -ne $true -or
115122
$result.ResultFilePresent -eq $false -or
116-
$result.Tests -eq 0 -or
117-
$result.Passed -eq 0 -or
118-
$result.Failed -gt 0 -or
119-
$result.Inconclusive -gt 0
123+
($hasTestsValue -and $result.Tests -eq 0) -or
124+
($hasPassedValue -and $hasTestsValue -and $result.Tests -gt 0 -and $result.Passed -eq 0) -or
125+
($hasFailedValue -and $result.Failed -gt 0) -or
126+
($hasInconclusiveValue -and $result.Inconclusive -gt 0) -or
127+
($hasNotRunValue -and $result.NotRun -gt 0)
120128
)
121129

122130
if ($testFailure) {
@@ -133,16 +141,54 @@ foreach ($expected in $expectedTestSuites) {
133141
$logGroupName = $expected.Name -replace '-TestResult-Report.*', ''
134142

135143
LogGroup " - $color$logGroupName$reset" {
144+
$failureReasons = [System.Collections.Generic.List[string]]::new()
145+
146+
if ($result.ResultFilePresent -eq $false) {
147+
$missingResultFiles.Add($expected.Name)
148+
$null = $failureReasons.Add("Result file '$($expected.Name)' was not found in the artifacts.")
149+
}
150+
136151
if ($result.Executed -eq $false) {
137152
$unexecutedTests.Add($expected.Name)
138-
Write-GitHubError "Test was not executed as reported in file: $($expected.Name)"
139-
$totalErrors++
140-
} elseif ($result.Result -eq 'Failed') {
141-
$failedTests.Add($expected.Name)
142-
Write-GitHubError "Test result explicitly marked as Failed in file: $($expected.Name)"
153+
$null = $failureReasons.Add("Test execution flag is false in file: $($expected.Name)")
154+
}
155+
156+
if ($result.Result -and $result.Result -ne 'Passed') {
157+
if ($failedTests -notcontains $expected.Name) {
158+
$failedTests.Add($expected.Name)
159+
}
160+
$null = $failureReasons.Add("Test result value '$($result.Result)' indicates failure in file: $($expected.Name)")
161+
}
162+
163+
if ($hasTestsValue -and $result.Tests -eq 0) {
164+
$null = $failureReasons.Add("No tests were reported in file: $($expected.Name)")
165+
}
166+
167+
if ($hasPassedValue -and $hasTestsValue -and $result.Tests -gt 0 -and $result.Passed -eq 0) {
168+
$null = $failureReasons.Add("No tests passed according to file: $($expected.Name)")
169+
}
170+
171+
if ($hasFailedValue -and $result.Failed -gt 0) {
172+
if ($failedTests -notcontains $expected.Name) {
173+
$failedTests.Add($expected.Name)
174+
}
175+
$null = $failureReasons.Add("$($result.Failed) tests failed in file: $($expected.Name)")
176+
}
177+
178+
if ($hasInconclusiveValue -and $result.Inconclusive -gt 0) {
179+
$null = $failureReasons.Add("$($result.Inconclusive) tests were inconclusive in file: $($expected.Name)")
180+
}
181+
182+
if ($hasNotRunValue -and $result.NotRun -gt 0) {
183+
$null = $failureReasons.Add("$($result.NotRun) tests were not run in file: $($expected.Name)")
184+
}
185+
186+
foreach ($reason in $failureReasons) {
187+
Write-GitHubError $reason
143188
$totalErrors++
144189
}
145-
$result | Format-Table | Out-String
190+
191+
Write-Host "$($result | Format-Table | Out-String)"
146192
}
147193

148194
if ($result.ResultFilePresent) {
@@ -181,6 +227,10 @@ LogGroup " - $color`Summary$reset" {
181227
Write-Host 'Unexecuted Test Files'
182228
$unexecutedTests | ForEach-Object { Write-Host " - $_" }
183229
}
230+
if ($missingResultFiles.Count -gt 0) {
231+
Write-Host 'Missing Test Result Files'
232+
$missingResultFiles | ForEach-Object { Write-Host " - $_" }
233+
}
184234
}
185235

186236
exit $totalErrors

0 commit comments

Comments
 (0)