Skip to content

Commit

Permalink
Use test run statistics from provided data instead of calculating our…
Browse files Browse the repository at this point in the history
…selves
  • Loading branch information
Tyrrrz committed Jun 10, 2022
1 parent 7e95bb7 commit 04fe779
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
15 changes: 14 additions & 1 deletion GitHubActionsTestLogger/TestLoggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,21 @@ public void HandleTestRunComplete(TestRunCompleteEventArgs args)
_testRunCriteria?.TryGetTargetFramework() ??
"Unknown Target Framework";

var testRunStatistics = new TestRunStatistics(
args.TestRunStatistics[TestOutcome.Passed],
args.TestRunStatistics[TestOutcome.Failed],
args.TestRunStatistics[TestOutcome.Skipped],
args.TestRunStatistics.ExecutedTests,
args.ElapsedTimeInRunningTests
);

_github.CreateSummary(
TestSummary.Generate(testSuiteName, targetFrameworkName, _testResults)
TestSummary.Generate(
testSuiteName,
targetFrameworkName,
testRunStatistics,
_testResults
)
);
}
}
Expand Down
11 changes: 11 additions & 0 deletions GitHubActionsTestLogger/TestRunStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace GitHubActionsTestLogger;

internal record TestRunStatistics(
long PassedTestCount,
long FailedTestCount,
long SkippedTestCount,
long TotalTestCount,
TimeSpan ElapsedDuration
);
28 changes: 11 additions & 17 deletions GitHubActionsTestLogger/TestSummary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GitHubActionsTestLogger.Utils.Extensions;
Expand All @@ -12,21 +11,16 @@ internal static class TestSummary
public static string Generate(
string testSuiteName,
string targetFrameworkName,
TestRunStatistics testRunStatistics,
IReadOnlyList<TestResult> testResults)
{
var passedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Passed);
var failedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Failed);
var skippedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Skipped);
var totalTestCount = testResults.Count;
var totalTestDuration = TimeSpan.FromMilliseconds(testResults.Sum(t => t.Duration.TotalMilliseconds));

var buffer = new StringBuilder();

// Spoiler header
buffer
.Append("<details>")
.Append("<summary>")
.Append(failedTestCount <= 0 ? "🟢" : "🔴")
.Append(testRunStatistics.FailedTestCount <= 0 ? "🟢" : "🔴")
.Append(" ")
.Append("<b>")
.Append(testSuiteName)
Expand Down Expand Up @@ -76,30 +70,30 @@ public static string Generate(
.Append("<tr>")
.Append("<td align=\"center\">")
.Append(
passedTestCount > 0
? passedTestCount.ToString()
testRunStatistics.PassedTestCount > 0
? testRunStatistics.PassedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(
failedTestCount > 0
? failedTestCount.ToString()
testRunStatistics.FailedTestCount > 0
? testRunStatistics.FailedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(
skippedTestCount > 0
? skippedTestCount.ToString()
testRunStatistics.SkippedTestCount > 0
? testRunStatistics.SkippedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(totalTestCount)
.Append(testRunStatistics.TotalTestCount)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(totalTestDuration.ToHumanString())
.Append(testRunStatistics.ElapsedDuration.ToHumanString())
.Append("</td>")
.Append("</tr>")
.Append("</table>")
Expand Down

0 comments on commit 04fe779

Please sign in to comment.