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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Enhanced.Testing.Component" Version="1.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enhanced.Testing.Component" Version="1.6.0" />
                    
Directory.Packages.props
<PackageReference Include="Enhanced.Testing.Component" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Enhanced.Testing.Component --version 1.6.0
                    
#r "nuget: Enhanced.Testing.Component, 1.6.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Enhanced.Testing.Component@1.6.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Enhanced.Testing.Component&version=1.6.0
                    
Install as a Cake Addin
#tool nuget:?package=Enhanced.Testing.Component&version=1.6.0
                    
Install as a Cake Tool

Enhanced.Testing.Component

NuGet

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 as ConnectionStrings:{ConnectionName}, so the app needs no test-specific configuration.
  • Image: the container image (each harness has a pinned DefaultImage).
  • ConfigureContainer: customizes the Testcontainers builder. It runs after the harness has applied its own settings, so it can override them.
  • StartConcurrently: set to false to 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.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.6.0 94 11/22/2026
1.5.0 96 11/20/2026
1.4.0 299 11/13/2026
1.3.0 239 11/12/2026
1.2.4 2,352 3/4/2026
1.2.3 1,295 10/18/2024
1.2.2 374 8/7/2024
1.2.1 283 8/4/2024
1.2.0 320 8/1/2024
1.1.1 301 7/22/2024
1.1.0 322 7/21/2024
1.0.1 330 7/16/2024
1.0.0 283 6/12/2024