Skip to content

Commit c8dd4c3

Browse files
Improve testing templates (#7619)
* Improve testing templates Fixes #7606 * Add missing namespace & fix variable name
1 parent 9cf2511 commit c8dd4c3

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

src/Aspire.ProjectTemplates/templates/aspire-mstest/9.1/IntegrationTest1.cs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
using Microsoft.Extensions.Logging;
2+
13
namespace Aspire.Tests._1;
24

35
[TestClass]
46
public class IntegrationTest1
57
{
8+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
9+
610
// Instructions:
711
// 1. Add a project reference to the target AppHost project, e.g.:
812
//
@@ -17,19 +21,26 @@ public class IntegrationTest1
1721
// {
1822
// // Arrange
1923
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
24+
// appHost.Services.AddLogging(logging =>
25+
// {
26+
// logging.SetMinimumLevel(LogLevel.Debug);
27+
// // Override the logging filters from the app's configuration
28+
// logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug);
29+
// logging.AddFilter("Aspire.", LogLevel.Debug);
30+
// });
2031
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2132
// {
2233
// clientBuilder.AddStandardResilienceHandler();
2334
// });
24-
// await using var app = await appHost.BuildAsync();
25-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
26-
// await app.StartAsync();
27-
35+
//
36+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
37+
// await app.StartAsync().WaitAsync(DefaultTimeout);
38+
//
2839
// // Act
2940
// var httpClient = app.CreateHttpClient("webfrontend");
30-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
41+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3142
// var response = await httpClient.GetAsync("/");
32-
43+
//
3344
// // Assert
3445
// Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
3546
// }

src/Aspire.ProjectTemplates/templates/aspire-nunit/9.1/IntegrationTest1.cs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using Microsoft.Extensions.Logging;
2+
13
namespace Aspire.Tests._1;
24

35
public class IntegrationTest1
46
{
7+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
8+
59
// Instructions:
610
// 1. Add a project reference to the target AppHost project, e.g.:
711
//
@@ -16,19 +20,26 @@ public class IntegrationTest1
1620
// {
1721
// // Arrange
1822
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
23+
// appHost.Services.AddLogging(logging =>
24+
// {
25+
// logging.SetMinimumLevel(LogLevel.Debug);
26+
// // Override the logging filters from the app's configuration
27+
// logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug);
28+
// logging.AddFilter("Aspire.", LogLevel.Debug);
29+
// });
1930
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2031
// {
2132
// clientBuilder.AddStandardResilienceHandler();
2233
// });
23-
// await using var app = await appHost.BuildAsync();
24-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
25-
// await app.StartAsync();
26-
34+
//
35+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
36+
// await app.StartAsync().WaitAsync(DefaultTimeout);
37+
//
2738
// // Act
2839
// var httpClient = app.CreateHttpClient("webfrontend");
29-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
40+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3041
// var response = await httpClient.GetAsync("/");
31-
42+
//
3243
// // Assert
3344
// Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
3445
// }

src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests/WebTests.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using Microsoft.Extensions.Logging;
2+
13
namespace Aspire_StarterApplication._1.Tests;
24

35
#if (TestFramework == "MSTest")
46
[TestClass]
57
#endif
68
public class WebTests
79
{
10+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
11+
812
#if (TestFramework == "MSTest")
913
[TestMethod]
1014
#elif (TestFramework == "NUnit")
@@ -16,21 +20,27 @@ public async Task GetWebResourceRootReturnsOkStatusCode()
1620
{
1721
// Arrange
1822
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.GeneratedClassNamePrefix_AppHost>();
23+
appHost.Services.AddLogging(logging =>
24+
{
25+
logging.SetMinimumLevel(LogLevel.Debug);
26+
// Override the logging filters from the app's configuration
27+
logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug);
28+
logging.AddFilter("Aspire.", LogLevel.Debug);
29+
#if (TestFramework == "xUnit.net")
30+
// To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
31+
#endif
32+
});
1933
appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2034
{
2135
clientBuilder.AddStandardResilienceHandler();
2236
});
23-
#if (TestFramework == "xUnit.net")
24-
// To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
25-
#endif
2637

27-
await using var app = await appHost.BuildAsync();
28-
var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
29-
await app.StartAsync();
38+
await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
39+
await app.StartAsync().WaitAsync(DefaultTimeout);
3040

3141
// Act
3242
var httpClient = app.CreateHttpClient("webfrontend");
33-
await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
43+
await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3444
var response = await httpClient.GetAsync("/");
3545

3646
// Assert

src/Aspire.ProjectTemplates/templates/aspire-xunit/9.1/IntegrationTest1.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using Microsoft.Extensions.Logging;
2+
13
namespace Aspire.Tests._1.Tests;
24

35
public class IntegrationTest1
46
{
7+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
8+
59
// Instructions:
610
// 1. Add a project reference to the target AppHost project, e.g.:
711
//
@@ -16,21 +20,27 @@ public class IntegrationTest1
1620
// {
1721
// // Arrange
1822
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
23+
// appHost.Services.AddLogging(logging =>
24+
// {
25+
// logging.SetMinimumLevel(LogLevel.Debug);
26+
// // Override the logging filters from the app's configuration
27+
// logging.AddFilter(appHost.Environment.ApplicationName, LogLevel.Debug);
28+
// logging.AddFilter("Aspire.", LogLevel.Debug);
29+
// // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
30+
// });
1931
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2032
// {
2133
// clientBuilder.AddStandardResilienceHandler();
2234
// });
23-
// // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
2435
//
25-
// await using var app = await appHost.BuildAsync();
26-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
27-
// await app.StartAsync();
28-
36+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
37+
// await app.StartAsync().WaitAsync(DefaultTimeout);
38+
//
2939
// // Act
3040
// var httpClient = app.CreateHttpClient("webfrontend");
31-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
41+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3242
// var response = await httpClient.GetAsync("/");
33-
43+
//
3444
// // Assert
3545
// Assert.Equal(HttpStatusCode.OK, response.StatusCode);
3646
// }

0 commit comments

Comments
 (0)