From ae8f94557d914fff24b178f26d2d73b6cdaec4d4 Mon Sep 17 00:00:00 2001 From: Damian Edwards Date: Fri, 14 Feb 2025 11:09:30 -0800 Subject: [PATCH 1/2] Improve testing templates Fixes #7606 --- .../aspire-mstest/9.1/IntegrationTest1.cs | 21 +++++++++++++----- .../aspire-nunit/9.1/IntegrationTest1.cs | 21 +++++++++++++----- .../WebTests.cs | 22 +++++++++++++------ .../aspire-xunit/9.1/IntegrationTest1.cs | 22 +++++++++++++------ 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs index b3aa20241f1..a1a69053f36 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs @@ -3,6 +3,8 @@ namespace Aspire.Tests._1; [TestClass] public class IntegrationTest1 { + private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); + // Instructions: // 1. Add a project reference to the target AppHost project, e.g.: // @@ -17,19 +19,26 @@ public class IntegrationTest1 // { // // Arrange // var appHost = await DistributedApplicationTestingBuilder.CreateAsync(); + // appHost.Services.AddLogging(logging => + // { + // logging.SetMinimumLevel(LogLevel.Debug); + // // Override the logging filters from the app's configuration + // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter("Aspire.", LogLevel.Debug); + // }); // appHost.Services.ConfigureHttpClientDefaults(clientBuilder => // { // clientBuilder.AddStandardResilienceHandler(); // }); - // await using var app = await appHost.BuildAsync(); - // var resourceNotificationService = app.Services.GetRequiredService(); - // await app.StartAsync(); - + // + // await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout); + // await app.StartAsync().WaitAsync(DefaultTimeout); + // // // Act // var httpClient = app.CreateHttpClient("webfrontend"); - // await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); + // await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout); // var response = await httpClient.GetAsync("/"); - + // // // Assert // Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // } diff --git a/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs index 626cb2c564a..fa235c21e51 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs @@ -2,6 +2,8 @@ namespace Aspire.Tests._1; public class IntegrationTest1 { + private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); + // Instructions: // 1. Add a project reference to the target AppHost project, e.g.: // @@ -16,19 +18,26 @@ public class IntegrationTest1 // { // // Arrange // var appHost = await DistributedApplicationTestingBuilder.CreateAsync(); + // appHost.Services.AddLogging(logging => + // { + // logging.SetMinimumLevel(LogLevel.Debug); + // // Override the logging filters from the app's configuration + // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter("Aspire.", LogLevel.Debug); + // }); // appHost.Services.ConfigureHttpClientDefaults(clientBuilder => // { // clientBuilder.AddStandardResilienceHandler(); // }); - // await using var app = await appHost.BuildAsync(); - // var resourceNotificationService = app.Services.GetRequiredService(); - // await app.StartAsync(); - + // + // await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout); + // await app.StartAsync().WaitAsync(DefaultTimeout); + // // // Act // var httpClient = app.CreateHttpClient("webfrontend"); - // await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); + // await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout); // var response = await httpClient.GetAsync("/"); - + // // // Assert // Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); // } diff --git a/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs b/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs index d0b0d6f1cc0..dbffd41dfd4 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs @@ -5,6 +5,8 @@ namespace Aspire_StarterApplication._1.Tests; #endif public class WebTests { + private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); + #if (TestFramework == "MSTest") [TestMethod] #elif (TestFramework == "NUnit") @@ -16,21 +18,27 @@ public async Task GetWebResourceRootReturnsOkStatusCode() { // Arrange var appHost = await DistributedApplicationTestingBuilder.CreateAsync(); + appHost.Services.AddLogging(logging => + { + logging.SetMinimumLevel(LogLevel.Debug); + // Override the logging filters from the app's configuration + logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + logging.AddFilter("Aspire.", LogLevel.Debug); +#if (TestFramework == "xUnit.net") + // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging +#endif + }); appHost.Services.ConfigureHttpClientDefaults(clientBuilder => { clientBuilder.AddStandardResilienceHandler(); }); -#if (TestFramework == "xUnit.net") - // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging -#endif - await using var app = await appHost.BuildAsync(); - var resourceNotificationService = app.Services.GetRequiredService(); - await app.StartAsync(); + await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout); + await app.StartAsync().WaitAsync(DefaultTimeout); // Act var httpClient = app.CreateHttpClient("webfrontend"); - await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); + await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout); var response = await httpClient.GetAsync("/"); // Assert diff --git a/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs index dafcbf38f00..0980db6c698 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs @@ -2,6 +2,8 @@ namespace Aspire.Tests._1.Tests; public class IntegrationTest1 { + private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); + // Instructions: // 1. Add a project reference to the target AppHost project, e.g.: // @@ -16,21 +18,27 @@ public class IntegrationTest1 // { // // Arrange // var appHost = await DistributedApplicationTestingBuilder.CreateAsync(); + // appHost.Services.AddLogging(logging => + // { + // logging.SetMinimumLevel(LogLevel.Debug); + // // Override the logging filters from the app's configuration + // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter("Aspire.", LogLevel.Debug); + // // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging + // }); // appHost.Services.ConfigureHttpClientDefaults(clientBuilder => // { // clientBuilder.AddStandardResilienceHandler(); // }); - // // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging // - // await using var app = await appHost.BuildAsync(); - // var resourceNotificationService = app.Services.GetRequiredService(); - // await app.StartAsync(); - + // await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout); + // await app.StartAsync().WaitAsync(DefaultTimeout); + // // // Act // var httpClient = app.CreateHttpClient("webfrontend"); - // await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); + // await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout); // var response = await httpClient.GetAsync("/"); - + // // // Assert // Assert.Equal(HttpStatusCode.OK, response.StatusCode); // } From ab81df7821a8b76db066e2790bbc8dba8bc76d73 Mon Sep 17 00:00:00 2001 From: Damian Edwards Date: Fri, 14 Feb 2025 11:20:42 -0800 Subject: [PATCH 2/2] Add missing namespace & fix variable name --- .../templates/aspire-mstest/9.1/IntegrationTest1.cs | 4 +++- .../templates/aspire-nunit/9.1/IntegrationTest1.cs | 4 +++- .../9.1/Aspire-StarterApplication.1.Tests/WebTests.cs | 4 +++- .../templates/aspire-xunit/9.1/IntegrationTest1.cs | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs index a1a69053f36..b069bbcc1f6 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; + namespace Aspire.Tests._1; [TestClass] @@ -23,7 +25,7 @@ public class IntegrationTest1 // { // logging.SetMinimumLevel(LogLevel.Debug); // // Override the logging filters from the app's configuration - // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug); // logging.AddFilter("Aspire.", LogLevel.Debug); // }); // appHost.Services.ConfigureHttpClientDefaults(clientBuilder => diff --git a/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs index fa235c21e51..4b7997e116d 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; + namespace Aspire.Tests._1; public class IntegrationTest1 @@ -22,7 +24,7 @@ public class IntegrationTest1 // { // logging.SetMinimumLevel(LogLevel.Debug); // // Override the logging filters from the app's configuration - // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug); // logging.AddFilter("Aspire.", LogLevel.Debug); // }); // appHost.Services.ConfigureHttpClientDefaults(clientBuilder => diff --git a/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs b/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs index dbffd41dfd4..3a355ff95e1 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; + namespace Aspire_StarterApplication._1.Tests; #if (TestFramework == "MSTest") @@ -22,7 +24,7 @@ public async Task GetWebResourceRootReturnsOkStatusCode() { logging.SetMinimumLevel(LogLevel.Debug); // Override the logging filters from the app's configuration - logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug); logging.AddFilter("Aspire.", LogLevel.Debug); #if (TestFramework == "xUnit.net") // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging diff --git a/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs b/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs index 0980db6c698..d9c9c409c6a 100644 --- a/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs +++ b/src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; + namespace Aspire.Tests._1.Tests; public class IntegrationTest1 @@ -22,7 +24,7 @@ public class IntegrationTest1 // { // logging.SetMinimumLevel(LogLevel.Debug); // // Override the logging filters from the app's configuration - // logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug); + // logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug); // logging.AddFilter("Aspire.", LogLevel.Debug); // // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging // });