Skip to content

Commit 28894f7

Browse files
DamianEdwardsgithub-actions
authored and
github-actions
committed
Improve testing templates
Fixes #7606
1 parent 62d4086 commit 28894f7

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace Aspire.Tests._1;
33
[TestClass]
44
public class IntegrationTest1
55
{
6+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
7+
68
// Instructions:
79
// 1. Add a project reference to the target AppHost project, e.g.:
810
//
@@ -17,19 +19,26 @@ public class IntegrationTest1
1719
// {
1820
// // Arrange
1921
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
22+
// appHost.Services.AddLogging(logging =>
23+
// {
24+
// logging.SetMinimumLevel(LogLevel.Debug);
25+
// // Override the logging filters from the app's configuration
26+
// logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug);
27+
// logging.AddFilter("Aspire.", LogLevel.Debug);
28+
// });
2029
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2130
// {
2231
// clientBuilder.AddStandardResilienceHandler();
2332
// });
24-
// await using var app = await appHost.BuildAsync();
25-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
26-
// await app.StartAsync();
27-
33+
//
34+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
35+
// await app.StartAsync().WaitAsync(DefaultTimeout);
36+
//
2837
// // Act
2938
// var httpClient = app.CreateHttpClient("webfrontend");
30-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
39+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3140
// var response = await httpClient.GetAsync("/");
32-
41+
//
3342
// // Assert
3443
// Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
3544
// }

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace Aspire.Tests._1;
22

33
public class IntegrationTest1
44
{
5+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
6+
57
// Instructions:
68
// 1. Add a project reference to the target AppHost project, e.g.:
79
//
@@ -16,19 +18,26 @@ public class IntegrationTest1
1618
// {
1719
// // Arrange
1820
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
21+
// appHost.Services.AddLogging(logging =>
22+
// {
23+
// logging.SetMinimumLevel(LogLevel.Debug);
24+
// // Override the logging filters from the app's configuration
25+
// logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug);
26+
// logging.AddFilter("Aspire.", LogLevel.Debug);
27+
// });
1928
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2029
// {
2130
// clientBuilder.AddStandardResilienceHandler();
2231
// });
23-
// await using var app = await appHost.BuildAsync();
24-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
25-
// await app.StartAsync();
26-
32+
//
33+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
34+
// await app.StartAsync().WaitAsync(DefaultTimeout);
35+
//
2736
// // Act
2837
// var httpClient = app.CreateHttpClient("webfrontend");
29-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
38+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3039
// var response = await httpClient.GetAsync("/");
31-
40+
//
3241
// // Assert
3342
// Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
3443
// }

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Aspire_StarterApplication._1.Tests;
55
#endif
66
public class WebTests
77
{
8+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
9+
810
#if (TestFramework == "MSTest")
911
[TestMethod]
1012
#elif (TestFramework == "NUnit")
@@ -16,21 +18,27 @@ public async Task GetWebResourceRootReturnsOkStatusCode()
1618
{
1719
// Arrange
1820
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.GeneratedClassNamePrefix_AppHost>();
21+
appHost.Services.AddLogging(logging =>
22+
{
23+
logging.SetMinimumLevel(LogLevel.Debug);
24+
// Override the logging filters from the app's configuration
25+
logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug);
26+
logging.AddFilter("Aspire.", LogLevel.Debug);
27+
#if (TestFramework == "xUnit.net")
28+
// To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
29+
#endif
30+
});
1931
appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2032
{
2133
clientBuilder.AddStandardResilienceHandler();
2234
});
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
2635

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

3139
// Act
3240
var httpClient = app.CreateHttpClient("webfrontend");
33-
await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
41+
await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3442
var response = await httpClient.GetAsync("/");
3543

3644
// Assert

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace Aspire.Tests._1.Tests;
22

33
public class IntegrationTest1
44
{
5+
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
6+
57
// Instructions:
68
// 1. Add a project reference to the target AppHost project, e.g.:
79
//
@@ -16,21 +18,27 @@ public class IntegrationTest1
1618
// {
1719
// // Arrange
1820
// var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();
21+
// appHost.Services.AddLogging(logging =>
22+
// {
23+
// logging.SetMinimumLevel(LogLevel.Debug);
24+
// // Override the logging filters from the app's configuration
25+
// logging.AddFilter(builder.Environment.ApplicationName, LogLevel.Debug);
26+
// logging.AddFilter("Aspire.", LogLevel.Debug);
27+
// // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
28+
// });
1929
// appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
2030
// {
2131
// clientBuilder.AddStandardResilienceHandler();
2232
// });
23-
// // To output logs to the xUnit.net ITestOutputHelper, consider adding a package from https://www.nuget.org/packages?q=xunit+logging
2433
//
25-
// await using var app = await appHost.BuildAsync();
26-
// var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
27-
// await app.StartAsync();
28-
34+
// await using var app = await appHost.BuildAsync().WaitAsync(DefaultTimeout);
35+
// await app.StartAsync().WaitAsync(DefaultTimeout);
36+
//
2937
// // Act
3038
// var httpClient = app.CreateHttpClient("webfrontend");
31-
// await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30));
39+
// await app.ResourceNotifications.WaitForResourceHealthyAsync("webfrontend").WaitAsync(DefaultTimeout);
3240
// var response = await httpClient.GetAsync("/");
33-
41+
//
3442
// // Assert
3543
// Assert.Equal(HttpStatusCode.OK, response.StatusCode);
3644
// }

0 commit comments

Comments
 (0)