Lesson 91 of 93 · 6 min read · Updated
System Testing a Distributed App with .NET Aspire: AppHost and Aspire.Hosting.Testing
On this page
Testcontainers tests build their infrastructure by hand in a fixture: this container, that connection string, this migration. That is ideal for testing one piece of infrastructure at a time. A distributed application also needs a few tests that start the whole system the way a developer runs it locally. .NET Aspire describes that system in one AppHost project, and Aspire.Hosting.Testing can start the same AppHost from a test. This lesson builds a small AppHost and two end-to-end system tests on top of it.
Technical Requirements
- .NET 11 SDK
- Docker
- Aspire 17.0.0:
Aspire.Hosting.AppHost,Aspire.Hosting.Dotnet,Aspire.Hosting.PostgreSQL,Aspire.Hosting.RedisandAspire.Hosting.Testing - The Aspire CLI, installed with
dotnet tool install -g Aspire.Clior from https://get.aspire.dev - xUnit 2.9.3 and FluentAssertions 7.2.2
The AppHost: One Description of the System
The AppHost is the application's resource graph: which containers run, which connection strings the API receives, and who waits for whom.
M10.AppHost/AppHost.cs
A few lines deserve attention. WithReference(..., connectionName: "Postgres") injects the connection string as ConnectionStrings__Postgres, the name the API already reads, so the API has no dependency on Aspire. WaitFor makes the API start only after PostgreSQL and Redis are healthy. WithHttpHealthCheck("/health") tells Aspire how to decide that the API itself is healthy.
The AppHost uses the Aspire 17 "Project v2" model. The project file is a plain Microsoft.NET.Sdk project with an explicit Aspire.Hosting.AppHost package, instead of the older Aspire.AppHost.Sdk:
M10.AppHost/M10.AppHost.csproj
Package versions come from central package management, so the project file lists names only. The API is not a ProjectReference here; AddDotnetProject points to its .csproj by path.
Pitfall: in this model the orchestrator (DCP) and the dashboard come from the Aspire CLI bundle. If the CLI is not installed, the AppHost does not build. Install it on every developer machine and on every CI runner before the build step.
Letting the API Migrate Its Own Database
With Testcontainers, the test fixture applied the migrations. Under Aspire the database starts as an empty container, so the API applies migrations at startup when a flag is set. The AppHost sets it with Database__MigrateOnStartup=true:
M10.ContainerApi/Program.cs
The double underscore in the environment variable maps to the : separator in .NET configuration.
Starting the Whole AppHost from a Test
The test project references the AppHost project and uses DistributedApplicationTestingBuilder. The fixture starts everything once per test class, with a time limit:
M10.AppHost.Tests/AppHostSystemTests.cs
The same CancellationTokenSource limits the build, the start and the health wait together. On a CI runner with a slow network, the test fails after three minutes with a cancellation instead of blocking the pipeline. WaitForResourceHealthyAsync("api") is the observable condition: no sleeps, no polling loops in the test.
Projects.M10_AppHost is a type generated from the test project's reference to the AppHost project; the dot in the project name becomes an underscore. Three minutes is generous on purpose: the first run on a clean machine pulls the PostgreSQL and Redis images before anything can start.
The tests themselves call the API over real HTTP:
M10.AppHost.Tests/AppHostSystemTests.cs
CreateHttpClient("api") returns a client whose base address points to the API's endpoint, wherever Aspire placed it. The class carries a trait so the pipeline can select or exclude these slower system tests. A second test in the class simply checks that /health returns 200.
Testcontainers or Aspire?
| Testcontainers tests | Aspire system tests | |
|---|---|---|
| What starts | The containers the test asks for | The whole AppHost graph (containers and projects) |
| Where the setup lives | In the test fixture, by hand | In the AppHost, the same as local development |
| Speed | Fast (shared fixture) | Slower |
| When to use | Infrastructure behaviour, query fidelity | A small number of end-to-end system tests |
They answer different questions, and they can catch different bugs. A Testcontainers test finds that a query behaves differently on PostgreSQL. An Aspire test finds that the API reads the wrong connection string name, or that it starts before the database is ready. Keep many of the first kind and a few of the second.
Summary
- An Aspire AppHost describes containers, projects, connection strings and startup order in one place.
Aspire.Hosting.Testingstarts that same graph from a test, so system tests use the local development setup.- Limit startup with a
CancellationTokenSourceand wait forWaitForResourceHealthyAsyncinstead of sleeping. - Aspire 17 "Project v2" uses
Microsoft.NET.Sdk,AddDotnetProjectby path, and needs the Aspire CLI installed. - Use Testcontainers for focused infrastructure tests and Aspire for a few end-to-end system tests.