Skip to content

Commit

Permalink
Add templates for Aspire test projects (#2905)
Browse files Browse the repository at this point in the history
* Add templates for Aspire test projects

Fixes #579
  • Loading branch information
DamianEdwards committed Mar 15, 2024
1 parent cfc2998 commit befe821
Show file tree
Hide file tree
Showing 44 changed files with 423 additions and 16 deletions.
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
{
[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);
}
}
Loading

0 comments on commit befe821

Please sign in to comment.