Skip to content

About

Integration (component) tests for ASP.NET Core: the app in memory with WebApplicationFactory, real PostgreSQL, Kafka and Redis in Testcontainers.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Repository files navigation

Enhanced.Testing.Component

Integration (component) tests for ASP.NET Core with real dependencies: PostgreSQL, Kafka, Redis and more, in a few lines of code.

NuGet Build License .NET 8 | 9 | 10 | 11

Enhanced.Testing.Component runs your ASP.NET Core application in memory with WebApplicationFactory and starts the dependencies it needs in Docker with Testcontainers. A test calls the real application over HTTP or gRPC and checks what it wrote to a real database, topic or cache. Such tests are known as integration tests or component tests; the library replaces the fixture code that every project writes around them.

Why

  • Real dependencies. PostgreSQL, Kafka and Redis run in Docker via Testcontainers, and their connection strings reach your application through its normal configuration. No test-only code in the app.
  • One lifecycle. A component starts the containers, then your application in memory, and stops and removes everything at the end, including after a failed start.
  • Fast. Containers start in parallel, databases run without durability, Kafka uses the native image, and PostgreSQL template databases give each test its own copy in milliseconds.
  • Deterministic. Wait for your Kafka message instead of the next one, isolate data per test, and move time forward instead of sleeping.

Quick start

dotnet add package Enhanced.Testing.Component.PostgreSql
public class OrdersFixture : IAsyncLifetime
{
    private readonly IComponent _component;

    public OrdersFixture()
    {
        _component = ComponentBuilder.Create<Program>()
                                     .AddHarness(PostgreSql)
                                     .AddHarness(Http)
                                     .Build();
    }

    public PostgreSqlHarness PostgreSql { get; } = new() { ConnectionName = "Orders" };

    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 ShouldCreateOrder()
    {
        var response = await fixture.Http.CreateClient().PostAsJsonAsync("/orders", new { Item = "book" });

        response.EnsureSuccessStatusCode();
    }
}

The collection starts the fixture once for all test classes marked with [Collection("Orders")]. With IClassFixture<OrdersFixture> every test class would start its own containers and application; see Sharing a component between tests.

The test project references the application project. 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.

The application reads ConnectionStrings:Orders as usual and gets the container's connection string. See SampleServiceFixture for a fixture with PostgreSQL, Kafka, Redis and gRPC.

Packages

Package What it gives you NuGet
Enhanced.Testing.Component The component, HttpClientHarness, base classes for custom harnesses NuGet
Enhanced.Testing.Component.PostgreSql PostgreSQL container, per-test template databases NuGet
Enhanced.Testing.Component.DbContext EF Core: create the schema on start, query the application's database NuGet
Enhanced.Testing.Component.Kafka Kafka (native, Confluent) or Redpanda, producer and consumer harnesses, per-test topics NuGet
Enhanced.Testing.Component.Redis Redis or Redis Stack, direct database access, per-test databases NuGet
Enhanced.Testing.Component.GrpcClient gRPC clients that call the application in memory NuGet
Enhanced.Testing.Component.WireMock In-process fakes for external HTTP APIs and gRPC services NuGet
Enhanced.Testing.Component.Time A fake TimeProvider the test controls NuGet

Features

Each package has its own README with examples, which is also shown on nuget.org:

  • Lifecycle, customization, custom harnesses: parallel container start, rollback on failure, removing hosted services, your own WebApplicationFactory, image and builder options for every container.
  • Kafka: native, Confluent or Redpanda brokers, waiting for a specific message, a topic per test.
  • PostgreSQL and EF Core: schema on start, queries against the application's database, template databases, non-durable defaults.
  • Redis: direct database access, a database per fixture, flushing test data.
  • External HTTP APIs and gRPC services and time: WireMock stubs and a fake TimeProvider for timeouts, retries and timers.
  • gRPC: generated clients that call the application in memory.

Guides

Using with AI coding agents

The repository includes an Agent Skill that teaches coding agents how to write component tests with these packages: choosing packages, fixture templates, rules for fast and deterministic tests, and troubleshooting. It uses the open Agent Skills format, supported by Claude Code, Codex, GitHub Copilot, Cursor, Gemini CLI and others. Install it with the skills CLI (requires Node.js), which asks which agents to install it for:

npx skills add duskembayev/Enhanced.Testing.Component

Add -g to install it for all your projects, or -a <agent> (for example -a codex) to skip the agent prompt. To install it by hand, copy the skills/enhanced-testing-component folder into your agent's skills folder, for example .agents/skills/ or .claude/skills/ in your repository.

Agents without skill support can read llms.txt, an index of the documentation. Every package also ships XML documentation, which the skill tells agents how to find.

Requirements

  • .NET 8, 9, 10 or 11.
  • Docker for the container harnesses (PostgreSQL, Kafka, Redis). The HTTP, gRPC, WireMock and time harnesses don't need it.
  • Any test framework: the component only needs to be started and stopped, for example from an xUnit fixture.

Releases

Packages are published to nuget.org by CI when a vX.Y.Z tag is pushed. Versions follow semantic versioning, and package validation guards against breaking API changes within a major version. See Releases for the changelog.

License

MIT

About

Integration (component) tests for ASP.NET Core: the app in memory with WebApplicationFactory, real PostgreSQL, Kafka and Redis in Testcontainers.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages