Lesson 86 of 93 · 6 min read · Updated
Start Only the Infrastructure a Test Needs: Container Fixtures with Testcontainers
On this page
A real container costs time: the image must be pulled once, and every start takes seconds. A test suite that starts PostgreSQL, Redis, RabbitMQ and Kafka for every run makes a developer wait for all four, even when they only want to run one cache test. This lesson shows how to split container fixtures so each test class starts only the infrastructure its scenario needs, and how to configure each container so the setup stays predictable.
Technical Requirements
- .NET 11 SDK
- Docker
Testcontainers.PostgreSql,Testcontainers.Redis,Testcontainers.RabbitMqandTestcontainers.Kafka4.17.0StackExchange.Redis3.4.35,RabbitMQ.Client7.2.2,Confluent.Kafka2.16.0- xUnit 2.9.3
Group Containers by the Scenarios That Use Them
The ticket API in this example uses PostgreSQL for storage and Redis as a cache. Many tests need both: the cache-aside test reads from the database and writes to the cache. These two containers therefore live together in one fixture, InfrastructureFixture, and start in parallel:
M10.ContainerApi.Tests/Infrastructure/InfrastructureFixture.cs
M10.ContainerApi.Tests/Infrastructure/InfrastructureFixture.cs
Task.WhenAll starts both containers at the same time, so the fixture waits for the slower one instead of the sum of both. Each StartAsync() returns only when its wait strategy reports the service ready. The PostgreSQL module waits until the server accepts connections, so the first query never hits a database that is still booting. You never need Thread.Sleep to "give it a moment".
The message brokers are different. Only the RabbitMQ and Kafka test classes use them. If they were added to the same fixture, every database test would also wait for a Kafka broker to start. So each broker gets its own fixture:
M10.ContainerApi.Tests/Infrastructure/BrokerFixtures.cs
The RabbitMQ test class asks for this fixture with IClassFixture<RabbitMqFixture>. xUnit creates the fixture only when that class runs. If you filter the run to the Redis tests, RabbitMQ is never started.
| Fixture | Containers | Used by |
|---|---|---|
InfrastructureFixture (collection fixture) | PostgreSQL + Redis | Database, Respawn and cache tests |
RabbitMqFixture (class fixture) | RabbitMQ | RabbitMqMessagingTests only |
KafkaFixture (class fixture) | Kafka | KafkaMessagingTests only |
The rule behind this split is simple: the scenario decides the infrastructure, not the project. Ask which real services a test class must talk to, and give it exactly those. A fixture that starts "everything the application uses" feels convenient at first. As the system grows, it turns every small test run into a full environment start, and developers stop running the tests locally.
The broker fixture also creates the RabbitMQ connection once. Opening an AMQP connection is not free, so the tests share it and open cheap channels on it instead.
Pin Every Image Tag
All four builders receive an explicit tag: postgres:17-alpine, redis:7-alpine, rabbitmq:4.1-alpine and confluentinc/cp-kafka:7.9.0.
Common mistake: using latest or no tag at all. Then the image your tests run against changes whenever the vendor publishes a new release. One morning the suite fails, and nothing in your repository changed. A pinned tag makes the test environment part of your source code. You upgrade it on purpose, in a commit, and the CI run on that commit tells you whether the new version breaks anything. The same tags are reused in the CI workflow and in the Aspire AppHost, so every environment runs the same versions.
Configure the Container for Tests, Not for Production
A container's defaults are tuned for production. Sometimes a default makes tests slow or flaky, and the fixture is the place to change it. The Kafka fixture is a good example:
M10.ContainerApi.Tests/Infrastructure/BrokerFixtures.cs
Every Kafka test creates a new consumer group. Without this setting, each of them would wait three extra seconds before receiving its first message.
The fixture also creates topics explicitly instead of relying on automatic topic creation:
A test about message ordering needs to know how many partitions the topic has. If the broker decided, the test would depend on a value it never states.
The Redis connection is configured for tests too. The fixture sets AllowAdmin = true so it can run FLUSHDB between tests:
M10.ContainerApi.Tests/Infrastructure/InfrastructureFixture.cs
This is fine for a throwaway container. In production code, admin commands should stay disabled.
Summary
- Put containers that are used together in one fixture, and start them in parallel with
Task.WhenAll. - Give infrastructure that only a few test classes need its own
IClassFixture, so it starts only when those classes run. - Rely on
StartAsync()and its wait strategy instead of fixed delays. - Pin every image tag;
latestlets an outside release break your suite. - Tune container settings for tests in the fixture, such as Kafka's rebalance delay or explicit topic creation.