Enhanced.Testing.Component
1.6.0
dotnet add package Enhanced.Testing.Component --version 1.6.0
NuGet\Install-Package Enhanced.Testing.Component -Version 1.6.0
<PackageReference Include="Enhanced.Testing.Component" Version="1.6.0" />
<PackageVersion Include="Enhanced.Testing.Component" Version="1.6.0" />
<PackageReference Include="Enhanced.Testing.Component" />
paket add Enhanced.Testing.Component --version 1.6.0
#r "nuget: Enhanced.Testing.Component, 1.6.0"
#:package Enhanced.Testing.Component@1.6.0
#addin nuget:?package=Enhanced.Testing.Component&version=1.6.0
#tool nuget:?package=Enhanced.Testing.Component&version=1.6.0
Enhanced.Testing.Component
The core of Enhanced.Testing.Component, a library for
integration (component) tests of ASP.NET Core applications: a component that hosts your application in memory through
WebApplicationFactory together with its dependencies ("harnesses"), plus an HTTP client harness and the base classes
for writing your own harnesses.
dotnet add package Enhanced.Testing.Component
Usage
public class OrdersFixture : IAsyncLifetime
{
private readonly IComponent _component;
public OrdersFixture()
{
_component = ComponentBuilder.Create<Program>()
.AddHarness(Http)
.Build();
}
public HttpClientHarness Http { get; } = new();
public Task InitializeAsync() => _component.StartAsync(CancellationToken.None);
public Task DisposeAsync() => _component.StopAsync(CancellationToken.None);
}
[CollectionDefinition("Orders")]
public class OrdersCollection : ICollectionFixture<OrdersFixture>;
[Collection("Orders")]
public class OrdersTests(OrdersFixture fixture)
{
[Fact]
public async Task ShouldBeHealthy()
{
var response = await fixture.Http.CreateClient().GetAsync("/health");
response.EnsureSuccessStatusCode();
}
}
HttpClientHarness.CreateClient() returns an HttpClient that calls the application in memory through TestServer,
without opening network ports.
The collection starts the fixture once for all test classes marked with [Collection("Orders")]; with
IClassFixture<OrdersFixture> every test class would start its own application. See
Sharing a component between tests.
The test project references the application project, and Program must be visible to it: with top-level statements
before .NET 10, add <InternalsVisibleTo Include="MyApp.Tests" /> to the application project or declare
public partial class Program;. The example uses xUnit v2; in xUnit v3, InitializeAsync and DisposeAsync return
ValueTask.
Lifecycle
StartAsync starts all container harnesses in parallel, then the other harnesses in the order they were added, then
the application; a failed start stops everything again. StopAsync stops the application first, then the harnesses
in reverse order, and removes the containers. Add a harness after the harnesses it uses. If the application stops on
its own, for example because a BackgroundService threw, the component's members throw an
InvalidOperationException that names the failed service and carries its exception. See
Component lifecycle for details.
Container harnesses
PostgreSQL, Kafka and Redis harnesses run real containers via Testcontainers. They share these options:
ConnectionName: the container's connection string is passed to the application asConnectionStrings:{ConnectionName}, so the app needs no test-specific configuration.Image: the container image (each harness has a pinnedDefaultImage).ConfigureContainer: customizes the Testcontainers builder. It runs after the harness has applied its own settings, so it can override them.StartConcurrently: set tofalseto start the container in registration order instead of in parallel.
var postgreSql = new PostgreSqlHarness
{
ConnectionName = "Orders",
Image = "postgres:17",
ConfigureContainer = builder => builder
.WithEnvironment("TZ", "UTC")
.WithResourceMapping("init.sql", "/docker-entrypoint-initdb.d/")
};
Customizing the application under test
Remove background work the test does not need, or host the application through your own WebApplicationFactory
subclass (for example, one that overrides CreateHost or ConfigureWebHost):
var component = ComponentBuilder.Create(new MyApplicationFactory())
.RemoveHostedService<OutboxPublisher>()
.Configure(builder => builder.UseSetting("Features:Payments", "false"))
.AddHarness(httpClient)
.Build();
RemoveHostedService<TService> runs after the application's own registrations, so it removes hosted services added
in Program (and by Configure calls made before it). It matches the exact implementation type of non-keyed
registrations; a factory registration is matched only when the factory is declared to return TService, as
AddHostedService(sp => new OutboxPublisher(...)) is. The component does not dispose a factory passed to Create.
Custom harnesses
Derive from Harness to plug in any dependency, or from ContainerHarness<TContainer> for any Testcontainers
module. See Writing a custom harness.
Related packages
| Package | |
|---|---|
| Enhanced.Testing.Component.PostgreSql | PostgreSQL container, per-test template databases |
| Enhanced.Testing.Component.DbContext | EF Core schema and queries |
| Enhanced.Testing.Component.Kafka | Kafka or Redpanda, producer and consumer |
| Enhanced.Testing.Component.Redis | Redis or Redis Stack |
| Enhanced.Testing.Component.GrpcClient | gRPC clients |
| Enhanced.Testing.Component.WireMock | Fakes for external HTTP APIs |
| Enhanced.Testing.Component.Time | Fake TimeProvider |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Microsoft.AspNetCore.Mvc.Testing (>= 10.0.15)
- Testcontainers (>= 4.16.0)
-
net11.0
- Microsoft.AspNetCore.Mvc.Testing (>= 11.0.0)
- Testcontainers (>= 4.16.0)
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 8.0.34)
- Testcontainers (>= 4.16.0)
-
net9.0
- Microsoft.AspNetCore.Mvc.Testing (>= 9.0.23)
- Testcontainers (>= 4.16.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Enhanced.Testing.Component:
| Package | Downloads |
|---|---|
|
Enhanced.Testing.Component.Redis
Redis and Redis Stack containers with data isolation per fixture for integration (component) tests of ASP.NET Core applications. Part of Enhanced.Testing.Component. |
|
|
Enhanced.Testing.Component.GrpcClient
In-memory gRPC clients that call the application for integration (component) tests of ASP.NET Core applications. Part of Enhanced.Testing.Component. |
|
|
Enhanced.Testing.Component.PostgreSql
A PostgreSQL container with per-test template databases for integration (component) tests of ASP.NET Core applications. Part of Enhanced.Testing.Component. |
|
|
Enhanced.Testing.Component.DbContext
EF Core schema creation and access to the application's database for integration (component) tests of ASP.NET Core applications. Part of Enhanced.Testing.Component. |
|
|
Enhanced.Testing.Component.Kafka
Kafka and Redpanda containers, producers and consumers for integration (component) tests of ASP.NET Core applications. Part of Enhanced.Testing.Component. |
GitHub repositories
This package is not used by any popular GitHub repositories.