In this article, you learn how to create a test project, write tests, and run them for your .NET Aspire solutions. The tests in this article aren't unit tests, but rather functional or integration tests. .NET Aspire includes several variations of testing project templates that you can use to test your .NET Aspire resource dependencies—and their communications. The testing project templates are available for MSTest, NUnit, and xUnit testing frameworks and include a sample test that you can use as a starting point for your tests.
The easiest way to create a .NET Aspire test project is to use the testing project template. If you're starting a new .NET Aspire project and want to include test projects, the Visual Studio tooling supports that option. If you're adding a test project to an existing .NET Aspire project, you can use the dotnet new command to create a test project:
.NET CLI
dotnetnew aspire-xunit
.NET CLI
dotnetnew aspire-mstest
.NET CLI
dotnetnew aspire-nunit
For more information, see the .NET CLI dotnet new command documentation.
Explore the test project
The following example test project was created as part of the .NET Aspire Starter Application template. If you're unfamiliar with it, see Quickstart: Build your first .NET Aspire project. The .NET Aspire test project takes a project reference dependency on the target app host. Consider the template project:
The preceding project file is fairly standard. There's a PackageReference to the 📦 Aspire.Hosting.Testing NuGet package, which includes the required types to write tests for .NET Aspire projects.
The template test project includes a IntegrationTest1 class with a single test. The test verifies the following scenario:
The app host is successfully created and started.
The webfrontend resource is available and running.
An HTTP request can be made to the webfrontend resource and returns a successful response (HTTP 200 OK).
Consider the following test class:
C#
namespaceAspireApp.Tests;
publicclassIntegrationTest1
{
[Fact]
publicasync Task GetWebResourceRootReturnsOkStatusCode()
{
// Arrangevar appHost = await DistributedApplicationTestingBuilder
.CreateAsync<Projects.AspireApp_AppHost>();
appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
{
clientBuilder.AddStandardResilienceHandler();
});
// To output logs to the xUnit.net ITestOutputHelper, // consider adding a package from https://www.nuget.org/packages?q=xunit+loggingawaitusingvar app = await appHost.BuildAsync();
var resourceNotificationService = app.Services
.GetRequiredService<ResourceNotificationService>();
await app.StartAsync();
// Actvar httpClient = app.CreateHttpClient("webfrontend");
await resourceNotificationService.WaitForResourceAsync(
"webfrontend",
KnownResourceStates.Running
)
.WaitAsync(TimeSpan.FromSeconds(30));
var response = await httpClient.GetAsync("/");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
An HttpClient is created for the webfrontend resource by calling app.CreateHttpClient.
The resourceNotificationService is used to wait for the webfrontend resource to be available and running.
A simple HTTP GET request is made to the root of the webfrontend resource.
The test asserts that the response status code is OK.
Test resource environment variables
To further test resources and their expressed dependencies in your .NET Aspire solution, you can assert that environment variables are injected correctly. The following example demonstrates how to test that the webfrontend resource has an HTTPS environment variable that resolves to the apiservice resource:
The DistributedApplicationOperation.Publish argument is passed when calling GetEnvironmentVariableValuesAsync to specify environment variables that are published to the resource as binding expressions.
With the returned environment variables, the test asserts that the webfrontend resource has an HTTPS environment variable that resolves to the apiservice resource.
Summary
The .NET Aspire testing project template makes it easier to create test projects for .NET Aspire solutions. The template project includes a sample test that you can use as a starting point for your tests. The DistributedApplicationTestingBuilder follows a familiar pattern to the WebApplicationFactory<TEntryPoint> in ASP.NET Core. It allows you to create a test host for your distributed application and run tests against it.
Finally, when using the DistributedApplicationTestingBuilder all resource logs are redirected to the DistributedApplication by default. The redirection of resource logs enables scenarios where you want to assert that a resource is logging correctly.
Šī satura avotu var atrast vietnē GitHub, kur varat arī izveidot un pārskatīt problēmas un atgādāšanas pieprasījumus. Lai iegūtu papildinformāciju, skatiet mūsu līdzstrādnieku rokasgrāmatu.
.NET Aspire atsauksmes
.NET Aspire ir atklātā pirmkoda projekts. Atlasiet saiti, lai sniegtu atsauksmes:
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.