Skip to content

Commit beb9cfe

Browse files
authored
Refactor tests to include ITestOutputHelper for improved logging and diagnostics (#13270)
1 parent f9acbef commit beb9cfe

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

tests/Aspire.Hosting.Tests/AppHostSmokeTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Microsoft.DotNet.RemoteExecutor;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
10-
11-
//using Microsoft.Extensions.Hosting;
1210
using Microsoft.Extensions.Logging;
1311
using Microsoft.Extensions.Logging.Testing;
1412
using Microsoft.Extensions.Options;

tests/Aspire.Hosting.Tests/AsHttp2ServiceTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Aspire.Hosting.Utils;
5+
46
namespace Aspire.Hosting.Tests;
57

6-
public class AsHttp2ServiceTests
8+
public class AsHttp2ServiceTests(ITestOutputHelper testOutputHelper)
79
{
810
[Fact]
911
public void Http2TransportIsNotSetWhenHttp2ServiceAnnotationIsNotApplied()
@@ -57,5 +59,10 @@ public void Http2TransportIsNotAppliedToNonHttpEndpoints()
5759
Assert.Equal("http2", httpsBinding.Transport);
5860
}
5961

60-
private static TestProgram CreateTestProgram(string[] args) => TestProgram.Create<AsHttp2ServiceTests>(args, disableDashboard: true);
62+
private TestProgram CreateTestProgram(string[] args)
63+
{
64+
var program = TestProgram.Create<AsHttp2ServiceTests>(args, disableDashboard: true);
65+
program.AppBuilder.Services.AddTestAndResourceLogging(testOutputHelper);
66+
return program;
67+
}
6168
}

tests/Aspire.Hosting.Tests/ContainerTunnelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
namespace Aspire.Hosting.Tests;
1111

12-
public class ContainerTunnelTests
12+
public class ContainerTunnelTests(ITestOutputHelper testOutputHelper)
1313
{
1414
[Fact]
1515
[RequiresDocker]
1616
public async Task ContainerTunnelWorksWithYarp()
1717
{
1818
const string testName = "container-tunnel-works-with-yarp";
19-
using var builder = TestDistributedApplicationBuilder.Create();
19+
using var builder = TestDistributedApplicationBuilder.Create(testOutputHelper);
2020
builder.Configuration[KnownConfigNames.EnableContainerTunnel] = "true";
2121

2222
var servicea = builder.AddProject<Projects.ServiceA>($"{testName}-servicea");

tests/Aspire.Hosting.Tests/HealthCheckTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,9 @@ public async Task VerifyWithHttpHealthCheckBlocksDependentResources()
118118
[Fact]
119119
public async Task BuildThrowsOnMissingHealthCheckRegistration()
120120
{
121-
using var builder = TestDistributedApplicationBuilder.Create();
121+
using var builder = TestDistributedApplicationBuilder.Create(testOutputHelper);
122122

123123
builder.Services.AddLogging(b => {
124-
b.AddXunit(testOutputHelper);
125124
b.AddFakeLogging();
126125
});
127126

tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Aspire.Hosting.Tests;
1717

18-
public class ManifestGenerationTests
18+
public class ManifestGenerationTests(ITestOutputHelper testOutputHelper)
1919
{
2020
[Fact]
2121
public void EnsureAddParameterWithSecretFalseDoesntEmitSecretField()
@@ -91,7 +91,7 @@ public void EnsureWorkerProjectDoesNotGetBindingsGenerated()
9191
[Fact]
9292
public async Task WithContainerRegistryUpdatesContainerImageAnnotationsDuringPublish()
9393
{
94-
var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions
94+
var builder = CreateBuilder(new DistributedApplicationOptions
9595
{
9696
Args = GetManifestArgs(),
9797
ContainerRegistryOverride = "myprivateregistry.company.com"
@@ -113,7 +113,7 @@ public async Task WithContainerRegistryUpdatesContainerImageAnnotationsDuringPub
113113
[Fact]
114114
public void ExcludeLaunchProfileOmitsBindings()
115115
{
116-
var appBuilder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions
116+
var appBuilder = CreateBuilder(new DistributedApplicationOptions
117117
{ Args = GetJsonManifestArgs(), DisableDashboard = true, AssemblyName = typeof(ManifestGenerationTests).Assembly.FullName });
118118
var manifestStore = new JsonDocumentManifestStore();
119119
appBuilder.AddProject<Projects.ServiceA>("servicea", launchProfileName: null);
@@ -509,7 +509,7 @@ public void VerifyTestProgramFullManifest()
509509
[Fact]
510510
public async Task ParameterInputDefaultValuesGenerateCorrectly()
511511
{
512-
var appBuilder = DistributedApplication.CreateBuilder();
512+
var appBuilder = CreateBuilder();
513513
var param = appBuilder.AddParameter("param");
514514
param.Resource.Default = new GenerateParameterDefault()
515515
{
@@ -556,7 +556,7 @@ public async Task ParameterInputDefaultValuesGenerateCorrectly()
556556
[Fact]
557557
public async Task ContainerFilesAreWrittenToManifest()
558558
{
559-
var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions
559+
var builder = CreateBuilder(new DistributedApplicationOptions
560560
{
561561
Args = GetManifestArgs()
562562
});
@@ -598,7 +598,7 @@ public async Task ContainerFilesAreWrittenToManifest()
598598
[Fact]
599599
public async Task ContainerFilesWithMultipleSourcesAreWrittenToManifest()
600600
{
601-
var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions
601+
var builder = CreateBuilder(new DistributedApplicationOptions
602602
{
603603
Args = GetManifestArgs()
604604
});
@@ -642,7 +642,7 @@ public async Task ContainerFilesWithMultipleSourcesAreWrittenToManifest()
642642
[Fact]
643643
public async Task ContainerFilesWithMultipleDestinationsAreWrittenToManifest()
644644
{
645-
var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions
645+
var builder = CreateBuilder(new DistributedApplicationOptions
646646
{
647647
Args = GetManifestArgs()
648648
});
@@ -695,9 +695,10 @@ public async Task ContainerFilesWithMultipleDestinationsAreWrittenToManifest()
695695
Assert.Equal(expectedManifest, destManifest.ToString());
696696
}
697697

698-
private static TestProgram CreateTestProgramJsonDocumentManifestPublisher(bool includeIntegrationServices = false, bool includeNodeApp = false)
698+
private TestProgram CreateTestProgramJsonDocumentManifestPublisher(bool includeIntegrationServices = false, bool includeNodeApp = false)
699699
{
700700
var program = TestProgram.Create<ManifestGenerationTests>(GetJsonManifestArgs(), includeIntegrationServices, includeNodeApp);
701+
program.AppBuilder.Services.AddTestAndResourceLogging(testOutputHelper);
701702
program.AppBuilder.Pipeline.AddJsonDocumentManifestPublishing();
702703
return program;
703704
}
@@ -713,4 +714,18 @@ private static string[] GetManifestArgs()
713714
var manifestPath = Path.Combine(Path.GetTempPath(), "tempmanifests", Guid.NewGuid().ToString(), "manifest.json");
714715
return ["--operation", "publish", "--step", "publish-manifest", "--output-path", manifestPath];
715716
}
717+
718+
private IDistributedApplicationBuilder CreateBuilder(DistributedApplicationOptions options)
719+
{
720+
var builder = DistributedApplication.CreateBuilder(options);
721+
builder.Services.AddTestAndResourceLogging(testOutputHelper);
722+
return builder;
723+
}
724+
725+
private IDistributedApplicationBuilder CreateBuilder()
726+
{
727+
var builder = DistributedApplication.CreateBuilder();
728+
builder.Services.AddTestAndResourceLogging(testOutputHelper);
729+
return builder;
730+
}
716731
}

0 commit comments

Comments
 (0)