testcontainers

Spin up real Docker containers for databases and message brokers in .NET integration tests.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill testcontainers-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: testcontainers
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/testcontainers
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill testcontainers-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Mocking databases and message brokers in tests misses real SQL behavior, constraints, and integration bugs, giving false confidence. This Skill provides patterns for running actual infrastructure (SQL Server, PostgreSQL, MySQL, RabbitMQ, Kafka) in Docker containers during .NET integration tests. ## Core Features & Use Cases - Database Container Patterns: Spin up SQL Server, PostgreSQL, or MySQL containers with proper wait strategies, random ports, and migration testing. - Message Broker Testing: Test RabbitMQ and Kafka publish/consume flows against real broker containers, plus Azure Service Bus emulator setups. - Advanced Patterns: Multi-container networks, volume mounts, custom wait strategies, Respawn-based database resets, and CI/CD integration for GitHub Actions and Azure DevOps. - Use Case: You are writing tests for an order repository. Instead of mocking IDbConnection, start a real SQL Server container, run migrations, insert test data, and verify actual query behavior including constraints and transactions. ## Quick Start Ask the AI to write an xUnit integration test using Testcontainers that starts a PostgreSQL container and verifies inserting and retrieving a record from a real database.

Frequently Asked Questions about testcontainers

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use Testcontainers for .NET integration tests?▼

Create a TestcontainersContainer in your test class constructor with the desired Docker image, start it in InitializeAsync via IAsyncLifetime, and connect using the mapped public port. Dispose the container in DisposeAsync for automatic cleanup.

Should I use Testcontainers or mocks for database testing?▼

Use Testcontainers when you need to verify real SQL behavior, constraints, indexes, and migrations. Mocks cannot catch SQL syntax errors or schema mismatches, so they give false confidence for data access layer testing.

How do I reset database state between tests without recreating containers?▼

Use the Respawn library with a shared xUnit collection fixture. Respawn deletes table data in about 50ms while preserving schema and migrations, which is much faster than starting a new container per test.

Does Testcontainers work in CI/CD pipelines like GitHub Actions?▼

Yes, Testcontainers works in any CI environment with Docker available. GitHub Actions ubuntu-latest runners have Docker pre-installed, so integration tests run without extra setup.

Why do my Testcontainers tests fail with port conflicts?▼

Port conflicts happen when binding fixed host ports. Always use WithPortBinding(port, true) so Testcontainers assigns a random public port, which also enables safe parallel test execution.

How do I test Kafka or RabbitMQ with Testcontainers?▼

Start a RabbitMQ container with the rabbitmq:management-alpine image or Kafka with confluentinc/cp-kafka plus Zookeeper, then connect producers and consumers to the mapped ports. Wait strategies ensure the broker is ready before tests run.