Skip to content

Commit 80cbdf0

Browse files
rolfbjarneCopilot
andauthored
[tests] Fix Windows test summary always reporting 'All tests passed' (#24772)
Fix a copy-paste bug in create-windows-html-report.cs where both the success and failure branches wrote '# 🎉 All N tests passed 🎉'. The failure branch now generates a proper summary with <details>/## tags listing failed tests, matching the format TestResults.psm1 expects. Also fix TestResults.psm1 to emit <details> before ## (correct HTML nesting order), and avoid including misleading success-format file content in the failure section when the result file has no failure details. --------- Co-authored-by: Copilot <[email protected]>
1 parent 7e4857f commit 80cbdf0

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

scripts/create-windows-html-report/create-windows-html-report.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public static int Main (string [] args)
8585

8686
var indexContents = new StringBuilder ();
8787
var summaryContents = new StringBuilder ();
88+
var allFailedTests = new List<(string TrxName, TrxParser.TrxTestResult Test)> ();
89+
var failedTrxNames = new List<string> ();
90+
var passedTrxCount = 0;
91+
var failedTrxCount = 0;
8892

8993
indexContents.AppendLine ($"<!DOCTYPE html>");
9094
indexContents.AppendLine ($"<html>");
@@ -123,9 +127,12 @@ public static int Main (string [] args)
123127
var name = trx.Name;
124128
var path = trx.TestResults;
125129
var messageLines = new List<string> ();
130+
var trxSucceeded = true;
126131

127-
if (TrxParser.TryParseTrxFile (path, out var failedTests, out var outcome, out allTestsSucceeded, out var ex)) {
132+
if (TrxParser.TryParseTrxFile (path, out var failedTests, out var outcome, out trxSucceeded, out var ex)) {
128133
if (failedTests?.Any () == true) {
134+
foreach (var ft in failedTests)
135+
allFailedTests.Add ((name, ft));
129136
messageLines.Add (" <ul>");
130137
foreach (var ft in failedTests) {
131138
var testName = ft.Name;
@@ -152,7 +159,15 @@ public static int Main (string [] args)
152159
outcome = "Failed to parse test results";
153160
if (ex is not null)
154161
messageLines.Add ($"<div>{FormatHtml (ex.ToString ())}</div>");
162+
trxSucceeded = false;
163+
}
164+
165+
if (!trxSucceeded) {
155166
allTestsSucceeded = false;
167+
failedTrxNames.Add (name);
168+
failedTrxCount++;
169+
} else {
170+
passedTrxCount++;
156171
}
157172

158173
try {
@@ -194,7 +209,23 @@ public static int Main (string [] args)
194209
if (allTestsSucceeded) {
195210
summaryContents.AppendLine ($"# :tada: All {trxFiles.Length} tests passed :tada:");
196211
} else {
197-
summaryContents.AppendLine ($"# :tada: All {trxFiles.Length} tests passed :tada:");
212+
summaryContents.AppendLine ("# Test results");
213+
summaryContents.AppendLine ("<details>");
214+
summaryContents.AppendLine ($"<summary>{failedTrxCount} tests failed, {passedTrxCount} tests passed.</summary>");
215+
summaryContents.AppendLine ();
216+
summaryContents.AppendLine ("## Failed tests");
217+
summaryContents.AppendLine ();
218+
if (allFailedTests.Any ()) {
219+
foreach (var (trxName, test) in allFailedTests) {
220+
var msg = string.IsNullOrEmpty (test.Message) ? "" : $": {test.Message.Split ('\n') [0]}";
221+
summaryContents.AppendLine ($" * {trxName}/{test.Name}: {test.Outcome}{msg}");
222+
}
223+
} else {
224+
foreach (var trxName in failedTrxNames) {
225+
summaryContents.AppendLine ($" * {trxName}: Failed");
226+
}
227+
}
228+
summaryContents.AppendLine ("</details>");
198229
}
199230

200231
Directory.CreateDirectory (outputDirectory);

tools/devops/automation/scripts/TestResults.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@ Describe "TestResults tests" {
908908
909909
### :x: dotnettests tests (MacCatalyst)
910910
911-
<summary>5 tests failed, 6 tests passed.</summary>
912911
<details>
912+
<summary>5 tests failed, 6 tests passed.</summary>
913913
914914
</details>
915915
@@ -1002,9 +1002,9 @@ Describe "TestResults tests" {
10021002
10031003
### :x: dotnettests tests (MacCatalyst)
10041004
1005-
<summary>1 tests failed, 0 tests passed.</summary>
10061005
<details>
1007-
# :tada: All 5 tests passed :tada:
1006+
<summary>1 tests failed, 0 tests passed.</summary>
1007+
Test results reported success, but the tests job failed.
10081008
</details>
10091009
10101010
[Html Report (VSDrops)](vsdropsIndex/testStagedotnettests_maccatalyst-1/;/tests/vsdrops_index.html) [Download](/_apis/build/builds//artifacts?artifactName=HtmlReport-testStagedotnettests_maccatalyst-1&api-version=6.0&`$format=zip)

tools/devops/automation/scripts/TestResults.psm1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,21 @@ class ParallelTestsResults {
447447
$resultLines = @("Test has no summary file.")
448448
}
449449

450-
if ($addSummary) {
451-
$stringBuilder.AppendLine("<summary>$($result.Failed) tests failed, $($result.Passed) tests passed.</summary>")
452-
}
453450
if ($addDetails) {
454451
$stringBuilder.AppendLine("<details>")
455452
}
456-
if ($startLine -eq -1) {
457-
$startLine = 0
453+
if ($addSummary) {
454+
$stringBuilder.AppendLine("<summary>$($result.Failed) tests failed, $($result.Passed) tests passed.</summary>")
458455
}
459-
for ($i = $startLine; $i -lt $resultLines.Length; $i++) {
460-
$stringBuilder.AppendLine($resultLines[$i])
456+
if ($startLine -eq -1) {
457+
# No <details>, <summary>, or ## Failed tests found in the file.
458+
# The file likely has the success format (e.g. "# :tada: All N tests passed :tada:"),
459+
# which would be misleading in a failure section. Show a job failure message instead.
460+
$stringBuilder.AppendLine("Test results reported success, but the tests job failed.")
461+
} else {
462+
for ($i = $startLine; $i -lt $resultLines.Length; $i++) {
463+
$stringBuilder.AppendLine($resultLines[$i])
464+
}
461465
}
462466
if ($addDetails) {
463467
$stringBuilder.AppendLine("</details>")

0 commit comments

Comments
 (0)