Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templates for Aspire test projects #2905

Merged
merged 8 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/Aspire.Hosting/build/Aspire.Hosting.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<!-- Similar to ASP.NET Core and Worker apps, set the default CWD of orchestrator projects to be the project directory. -->
<RunWorkingDirectory Condition=" '$(RunWorkingDirectory)' == '' and '$(EnableDefaultRunWorkingDirectory)' != 'false' ">$(MSBuildProjectDirectory)</RunWorkingDirectory>
<AspireGeneratedClassesVisibility Condition="'$(AspireGeneratedClassesVisibility)' == ''">public</AspireGeneratedClassesVisibility>
<_AspireIntermediatePath>$(IntermediateOutputPath)Aspire\</_AspireIntermediatePath>
</PropertyGroup>

Expand Down Expand Up @@ -41,7 +42,7 @@ namespace Projects%3B
[global::System.CodeDom.Compiler.GeneratedCode("Aspire.Hosting", null)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Generated code.")]
[global::System.Diagnostics.DebuggerDisplay("Type = {GetType().Name,nq}, ProjectPath = {ProjectPath}")]
public class ]]>%(ClassName)<![CDATA[ : global::Aspire.Hosting.IProjectMetadata
]]>$(AspireGeneratedClassesVisibility)<![CDATA[ class ]]>%(ClassName)<![CDATA[ : global::Aspire.Hosting.IProjectMetadata
{
public string ProjectPath => """]]>%(ProjectPath)<![CDATA["""%3B
}]]>
Expand Down Expand Up @@ -82,8 +83,9 @@ namespace Projects%3B
[global::System.CodeDom.Compiler.GeneratedCode("Aspire.Hosting", null)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Generated code.")]
[global::System.Diagnostics.DebuggerDisplay("Type = {GetType().Name,nq}, ProjectPath = {ProjectPath}")]
internal static class ]]>%(ClassName)<![CDATA[
]]>$(AspireGeneratedClassesVisibility)<![CDATA[ class ]]>%(ClassName)<![CDATA[
{
private ]]>%(ClassName)<![CDATA[() { }
public static string ProjectPath => """]]>%(ProjectPath)<![CDATA["""%3B
}]]>
</Source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@
"UseRedisCache": {
"longName": "use-redis-cache",
"shortName": ""
},
"CreateTestsProject": {
"longName": "create-tests-project",
"shortName": "t"
}
},
"usageExamples": [
"--use-redis-cache"
"--use-redis-cache",
"--create-tests-project"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"id": "UseRedisCache",
"isVisible": true,
"persistenceScope": "templateGroup"
},
{
"id": "CreateTestsProject",
"isVisible": true,
"persistenceScope": "templateGroup"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"symbols/Framework/choices/net8.0/description": "Target net8.0",
"symbols/UseRedisCache/displayName": "_Use Redis for caching (requires Docker)",
"symbols/UseRedisCache/description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally.",
"symbols/CreateTestsProject/displayName": "Create a _tests project",
"symbols/CreateTestsProject/description": "Configures whether to create a project for integration tests using the AppHost project.",
"symbols/appHostHttpPort/description": "Port number to use for the HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostOtlpHttpPort/description": "Port number to use for the OTLP HTTP endpoint in launchSettings.json of the AppHost project.",
"symbols/appHostResourceHttpPort/description": "Port number to use for the resource service HTTP endpoint in launchSettings.json of the AppHost project.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"9FEB877E-015D-4E20-AE63-06C596E242E4",
"AC2DB38C-F5AD-4CEF-BC4C-04AE6EE86C9F",
"EB6E56D3-85C9-43D0-A65C-775F4C780950",
"98048C9C-BF28-46BA-A98E-63767EE5E3A8"
"98048C9C-BF28-46BA-A98E-63767EE5E3A8",
"BAAC2850-FDA3-40E4-8D41-6E024DDBC980"
],
"sources": [
{
Expand All @@ -42,6 +43,12 @@
"exclude": [
"*.sln"
]
},
{
"condition": "(!CreateTestsProject)",
"exclude": [
"AspireStarterApplication.1.Tests/**/*.*"
]
}
]
}
Expand Down Expand Up @@ -77,6 +84,13 @@
"displayName": "_Use Redis for caching (requires Docker)",
"description": "Configures whether to setup the application to use Redis for caching. Requires Docker to run locally."
},
"CreateTestsProject": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Create a _tests project",
"description": "Configures whether to create a project for integration tests using the AppHost project."
},
"appHostHttpPort": {
"type": "parameter",
"datatype": "integer",
Expand Down Expand Up @@ -330,6 +344,10 @@
},
{
"path": "AspireStarterApplication.1.Web\\AspireStarterApplication.1.Web.csproj"
},
{
"path": "AspireStarterApplication.1.Tests\\AspireStarterApplication.1.Tests.csproj",
"condition": "(CreateTestsProject)"
}
],
"postActions": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.Testing" Version="!!REPLACE_WITH_LATEST_VERSION!!" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AspireStarterApplication.1.AppHost\AspireStarterApplication.1.AppHost.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Aspire.Hosting.Testing" />
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Net;

namespace AspireStarterApplication.1.Tests;

public class WebTests
DamianEdwards marked this conversation as resolved.
Show resolved Hide resolved
{
[Fact]
public async Task GetWebResourceRootReturnsOkStatusCode()
{
// Arrange
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AspireStarterApplication.1_AppHost>();
await using var app = await appHost.BuildAsync();
await app.StartAsync();

// Act
var httpClient = app.CreateHttpClient("webfrontend");
var response = await httpClient.GetAsync("/");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice thing we do in our tests is:

var response = await _integrationServicesFixture.IntegrationServiceA.HttpGetAsync("http", $"/{resourceName}/verify");
var responseContent = await response.Content.ReadAsStringAsync();
Assert.True(response.IsSuccessStatusCode, responseContent);

That way if it fails, you get the responseContent logged.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we have resiliency on the HttpClient returned from CreateHttpClient, you can't actually do it quite like that, as if the status code wasn't a success status code, it will retry until it fails anyway. I think keeping this test in the template simple is probably best for now.

Copy link
Member

@eerhardt eerhardt Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't resilience only retry on certain failures? If a service returns 500 (Internal Server Error), it wouldn't retry that, would it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm not sure, I thought a 500 is exactly the kind of thing it would retry, vs. a 300 or 400 status code. Obviously connection level issues would be retried too.

}
}
Loading