Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TUnit.Engine/Extensions/TestApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
var htmlReporter = new Reporters.Html.HtmlReporter(extension);
var htmlReporterCommandProvider = new HtmlReporterCommandProvider(extension);

htmlReporter.SetGitHubReporter(githubReporter);

testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(CreateCapabilities(serviceProvider)),
(capabilities, serviceProvider) => new TUnitTestFramework(extension, serviceProvider, capabilities));
Expand Down
21 changes: 20 additions & 1 deletion TUnit.Engine/Reporters/GitHubReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
x.Value.TestNode.Properties.AsEnumerable().Any(p => p is InProgressTestNodeStateProperty)).ToArray();

var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"### {Assembly.GetEntryAssembly()?.GetName().Name} ({targetFramework})");

var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;

if (!string.IsNullOrEmpty(ArtifactUrl))
{
stringBuilder.AppendLine($"### {assemblyName} ({targetFramework}) [(View Report)]({ArtifactUrl})");
}
else
{
stringBuilder.AppendLine($"### {assemblyName} ({targetFramework})");
}

if (!string.IsNullOrEmpty(Filter))
{
Expand Down Expand Up @@ -167,6 +177,12 @@ public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
stringBuilder.AppendLine($"| {inProgress.Length} | In Progress (never completed) |");
}

if (string.IsNullOrEmpty(ArtifactUrl))
{
stringBuilder.AppendLine();
stringBuilder.AppendLine("> **Tip:** You can have HTML reports uploaded automatically as artifacts. [Learn more](https://tunit.dev/docs/guides/html-report#enabling-automatic-artifact-upload)");
}

if (passedCount == last.Count)
{
return WriteFile(stringBuilder.ToString());
Expand Down Expand Up @@ -370,6 +386,9 @@ private static string GetStatus(IProperty? stateProperty)

public string? Filter { get; set; }

// Set by HtmlReporter during OnTestSessionFinishingAsync, which MTP invokes before AfterRunAsync.
internal string? ArtifactUrl { get; set; }

internal void SetReporterStyle(GitHubReporterStyle style)
{
_reporterStyle = style;
Expand Down
17 changes: 15 additions & 2 deletions TUnit.Engine/Reporters/Html/HtmlReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal sealed class HtmlReporter(IExtension extension) : IDataConsumer, IDataP
private IMessageBus? _messageBus;
private string _resultsDirectory = "TestResults";
private readonly ConcurrentDictionary<string, ConcurrentQueue<TestNodeUpdateMessage>> _updates = [];
private GitHubReporter? _githubReporter;

#if NET
private ActivityCollector? _activityCollector;
Expand Down Expand Up @@ -179,6 +180,11 @@ internal void SetMessageBus(IMessageBus? messageBus)
_messageBus = messageBus;
}

internal void SetGitHubReporter(GitHubReporter githubReporter)
{
_githubReporter = githubReporter;
}

// Called by the AddTestSessionLifetimeHandler factory at startup, before any session events fire,
// so _resultsDirectory is guaranteed to be set before OnTestSessionFinishingAsync is invoked.
internal void SetResultsDirectory(string path)
Expand Down Expand Up @@ -648,7 +654,7 @@ private static bool IsFileLocked(IOException exception)
exception.Message.Contains("access denied", StringComparison.OrdinalIgnoreCase);
}

private static async Task TryGitHubIntegrationAsync(string filePath, CancellationToken cancellationToken)
private async Task TryGitHubIntegrationAsync(string filePath, CancellationToken cancellationToken)
{
if (Environment.GetEnvironmentVariable(EnvironmentConstants.GitHubActions) is not "true")
{
Expand Down Expand Up @@ -697,7 +703,14 @@ private static async Task TryGitHubIntegrationAsync(string filePath, Cancellatio

if (artifactId is not null && !string.IsNullOrEmpty(repo) && !string.IsNullOrEmpty(runId))
{
line = $"\n\ud83d\udcca [{assemblyName} — View HTML Report](https://github.com/{repo}/actions/runs/{runId}/artifacts/{artifactId})\n";
var artifactUrl = $"https://github.com/{repo}/actions/runs/{runId}/artifacts/{artifactId}";

if (_githubReporter is not null)
{
_githubReporter.ArtifactUrl = artifactUrl;
}

line = $"\n\ud83d\udcca [{assemblyName} — View HTML Report]({artifactUrl})\n";
}
else
{
Expand Down
Loading