Skip to content

Commit d116c4a

Browse files
radicaldanmoseley
andauthored
[CI] Test summary reporter improvements (#9263)
* GenerateTestReport: add logging, add artifacts url * Fix GenerateTestReport run on windows * Update tools/GenerateTestSummary/TestSummaryGenerator.cs Co-authored-by: Dan Moseley <danmose@microsoft.com> * address review feedback from @ danmoseley * Address review feedback from @ russkie --------- Co-authored-by: Dan Moseley <danmose@microsoft.com>
1 parent 923b9a5 commit d116c4a

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

.github/workflows/run-tests.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,8 @@ jobs:
300300
if: always()
301301
env:
302302
CI: false
303-
run: >
304-
test -d ${{ github.workspace }}/testresults &&
305-
${{ github.workspace }}/dotnet.sh
306-
run
307-
--project ${{ github.workspace }}/tools/GenerateTestSummary/GenerateTestSummary.csproj
308-
--
309-
${{ github.workspace }}/testresults
310-
-u ${{ steps.upload-logs.outputs.artifact-url }}
303+
shell: pwsh
304+
run: |
305+
if (Test-Path "${{ github.workspace }}/testresults") {
306+
& ${{ env.DOTNET_SCRIPT }} run --project "${{ github.workspace }}/tools/GenerateTestSummary/GenerateTestSummary.csproj" -- "${{ github.workspace }}/testresults" -u "${{ steps.upload-logs.outputs.artifact-url }}"
307+
}

tools/GenerateTestSummary/Program.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@
2727
var dirPathOrTrxFilePath = result.GetValue<string>(dirPathOrTrxFilePathArgument);
2828
if (string.IsNullOrEmpty(dirPathOrTrxFilePath))
2929
{
30-
Console.WriteLine("Please provide a directory path with trx files or a trx file path.");
30+
Console.WriteLine("Error: Please provide a directory path with trx files or a trx file path.");
3131
return;
3232
}
3333

3434
var combinedSummary = result.GetValue<bool>(combinedSummaryOption);
35+
var url = result.GetValue<string>(urlOption);
36+
37+
if (combinedSummary && !string.IsNullOrEmpty(url))
38+
{
39+
Console.WriteLine("Error: --url option is not supported with --combined option.");
40+
return;
41+
}
3542

3643
string report;
3744
if (combinedSummary)
@@ -43,15 +50,22 @@
4350
var reportBuilder = new StringBuilder();
4451
if (Directory.Exists(dirPathOrTrxFilePath))
4552
{
46-
var trxFiles = Directory.EnumerateFiles(dirPathOrTrxFilePath, "*.trx", SearchOption.AllDirectories);
47-
foreach (var trxFile in trxFiles)
53+
var trxFiles = Directory.EnumerateFiles(dirPathOrTrxFilePath, "*.trx", SearchOption.AllDirectories).ToList();
54+
if (trxFiles.Count == 0)
55+
{
56+
Console.WriteLine($"Warning: No trx files found in directory: {dirPathOrTrxFilePath}");
57+
}
58+
else
4859
{
49-
TestSummaryGenerator.CreateSingleTestSummaryReport(trxFile, reportBuilder);
60+
foreach (var trxFile in trxFiles)
61+
{
62+
TestSummaryGenerator.CreateSingleTestSummaryReport(trxFile, reportBuilder, url);
63+
}
5064
}
5165
}
5266
else
5367
{
54-
TestSummaryGenerator.CreateSingleTestSummaryReport(dirPathOrTrxFilePath, reportBuilder, result.GetValue<string>(urlOption));
68+
TestSummaryGenerator.CreateSingleTestSummaryReport(dirPathOrTrxFilePath, reportBuilder, url);
5569
}
5670

5771
report = reportBuilder.ToString();
@@ -73,12 +87,11 @@
7387
if (Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true"
7488
&& Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY") is string summaryPath)
7589
{
90+
Console.WriteLine($"Detected GitHub Actions environment. Writing to {summaryPath}");
7691
File.WriteAllText(summaryPath, report);
7792
}
78-
else
79-
{
80-
Console.WriteLine(report);
81-
}
93+
94+
Console.WriteLine(report);
8295
});
8396

8497
return rootCommand.Parse(args).Invoke();

tools/GenerateTestSummary/TestSummaryGenerator.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static string CreateCombinedTestSummaryReport(string basePath)
6666
? "lin"
6767
: filePath.Contains("macos-")
6868
? "mac"
69-
: "os?";
69+
: throw new InvalidOperationException($"Could not determine OS from file path: {filePath}");
7070

7171
tableBuilder.AppendLine(CultureInfo.InvariantCulture, $"| {(failed > 0 ? "❌" : "✅")} [{os}] {GetTestTitle(filePath)} | {passed} | {failed} | {skipped} | {total} |");
7272
}
@@ -84,7 +84,7 @@ public static string CreateCombinedTestSummaryReport(string basePath)
8484
return overallTableBuilder.ToString();
8585
}
8686

87-
public static void CreateSingleTestSummaryReport(string trxFilePath, StringBuilder reportBuilder, string? url = null)
87+
public static void CreateSingleTestSummaryReport(string trxFilePath, StringBuilder reportBuilder, string? url)
8888
{
8989
if (!File.Exists(trxFilePath))
9090
{
@@ -109,21 +109,27 @@ public static void CreateSingleTestSummaryReport(string trxFilePath, StringBuild
109109
var failed = counters.Failed;
110110
if (failed == 0)
111111
{
112+
Console.WriteLine($"No failed tests in {trxFilePath}");
112113
return;
113114
}
114115

115116
var total = counters.Total;
116117
var passed = counters.Passed;
117118
var skipped = counters.NotExecuted;
118119

119-
reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"### {GetTestTitle(trxFilePath)}");
120+
var title = string.IsNullOrEmpty(url)
121+
? GetTestTitle(trxFilePath)
122+
: $"{GetTestTitle(trxFilePath)} (<a href=\"{url}\">Logs</a>)";
123+
124+
reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"### {title}");
120125
reportBuilder.AppendLine("| Passed | Failed | Skipped | Total |");
121126
reportBuilder.AppendLine("|--------|--------|---------|-------|");
122127
reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"| {passed} | {failed} | {skipped} | {total} |");
123128

124129
reportBuilder.AppendLine();
125130
if (testRun.Results?.UnitTestResults is null)
126131
{
132+
Console.WriteLine($"Could not find any UnitTestResult entries in {trxFilePath}");
127133
return;
128134
}
129135

@@ -132,18 +138,20 @@ public static void CreateSingleTestSummaryReport(string trxFilePath, StringBuild
132138
{
133139
foreach (var test in failedTests)
134140
{
135-
var title = string.IsNullOrEmpty(url)
136-
? $"🔴 <b>{test.TestName}</b>"
137-
: $"🔴 <a href=\"{url}\">{test.TestName}</a>";
138-
139141
reportBuilder.AppendLine("<div>");
140142
reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"""
141-
<details><summary>{title}</summary>
143+
<details><summary>🔴 <b>{test.TestName}</b></summary>
144+
142145
""");
143146

144147
var errorMsgBuilder = new StringBuilder();
145148
errorMsgBuilder.AppendLine(test.Output?.ErrorInfo?.InnerText ?? string.Empty);
146-
errorMsgBuilder.AppendLine(test.Output?.StdOut ?? string.Empty);
149+
if (test.Output?.StdOut is not null)
150+
{
151+
errorMsgBuilder.AppendLine();
152+
errorMsgBuilder.AppendLine("### StdOut");
153+
errorMsgBuilder.AppendLine(test.Output.StdOut);
154+
}
147155

148156
// Truncate long error messages for readability
149157
var errorMsgTruncated = TruncateTheStart(errorMsgBuilder.ToString(), 50_000);

0 commit comments

Comments
 (0)