/iac-automation-skills:testcontainers-integration-testing-configurationfrom iac-automation-skills
Guides configuring Testcontainers for integration tests with real Postgres, Kafka, Redis containers, covering lifecycle management, wait strategies, and CI runner considerations.
Ships in iac-automation-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A unit test that mocks the database driver proves the code calls the mock correctly, not that it works against a real Postgres — a subtly wrong SQL query, an index assumption that doesn't hold, or a driver-specific type-mapping quirk routinely passes every mocked test and fails only in a real environment. Testcontainers closes that gap by programmatically starting real, disposable Docker containers (a real Postgres, a real Kafka broker, a real Redis instance) scoped to a test run, so integration tests exercise the actual dependency instead of a stand-in for it — without a hand-maintained shared staging database that tests can corrupt for each other or drift out of sync with production versions. This skill covers container lifecycle management (per-test vs. module-scoped/singleton containers), wait strategies (waiting for a container to actually be ready, not just started), and the CI-runner resource and Docker-availability considerations that most commonly break a Testcontainers suite that works fine on a developer's laptop.
testcontainers-java
with JUnit 5, testcontainers-python, testcontainers-go,
testcontainers-node, or the corresponding module for the test
framework in use) plus the specific module for each dependency
(e.g. testcontainers-java's postgresql, kafka, and redis
modules ship pre-built wait-strategy and configuration support rather
than requiring a hand-rolled generic container setup).Start a container scoped to the narrowest safe lifecycle — a fresh container per test class (or per test module/suite) is usually the right default; a fresh container per individual test method is correct only when tests genuinely need full isolation and is otherwise a significant, often unnecessary, speed cost:
// JUnit 5 + testcontainers-java: container shared across all tests
// in this class, started once
@Testcontainers
class OrderRepositoryTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16")
.withDatabaseName("orders_test")
.withUsername("test")
.withPassword("test");
@Test
void savesAndLoadsAnOrder() {
// each test runs against the same container; state resets via
// a transaction rollback or a schema-reset step (see step 3),
// not by restarting the container per test
}
}
Use a module-specific wait strategy, not a fixed sleep, so tests start the moment the dependency is actually ready rather than after an arbitrarily guessed delay:
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.0"))
.waitingFor(Wait.forListeningPort());
# testcontainers-python: Postgres module's built-in readiness wait
from testcontainers.postgres import PostgresContainer
postgres = PostgresContainer("postgres:16")
postgres.start() # blocks until the container reports ready, not a fixed sleep
A fixed sleep(10) "wait strategy" is both slower than necessary on a
fast machine and still flaky on a slow/loaded one (CI runners under
contention are exactly where a too-short fixed sleep fails
intermittently) — always prefer the module's built-in readiness check
(log-line match, port-open check, or an actual health-check query)
over a fixed delay.
Reset state between tests deliberately, rather than assuming a shared, class-scoped container implies test isolation for free:
@BeforeEach
void resetSchema() {
jdbcTemplate.execute("TRUNCATE orders, order_items RESTART IDENTITY CASCADE");
}
Whether reset happens via a transaction rolled back after each test,
a TRUNCATE/schema-reset step, or a fresh logical database/topic per
test is a deliberate design decision — the wrong assumption here
(that a shared container automatically means shared, leaking state
across tests) is the most common cause of order-dependent test
flakiness in a Testcontainers suite.
Wire the container's dynamically assigned port into the application/config under test, never a hardcoded port — Testcontainers maps container ports to a random free host port by default specifically to allow parallel test runs without collisions:
@DynamicPropertySource
static void registerPgProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
engine = create_engine(postgres.get_connection_url())
A hardcoded port (e.g. always mapping to host port 5432) breaks
parallel test execution the moment two test runs (two CI jobs, two
developers) try to bind the same host port simultaneously.
Use a singleton/shared container pattern across test classes
when many test classes need the same dependency and per-class startup
cost dominates suite runtime — start the container once, in a static
initializer with no explicit stop(), and let Testcontainers' Ryuk
reaper clean it up when the JVM/test process exits:
public abstract class AbstractIntegrationTest {
static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16").withReuse(true);
static {
postgres.start(); // started once, deliberately never stopped here
}
}
withReuse(true) (paired with
testcontainers.reuse.enable=true in ~/.testcontainers.properties)
goes further, keeping the same container running across separate
test-runner invocations on a developer's machine for fast local
iteration — this specific setting is a local developer-experience
optimization and should not be relied on for CI correctness, since a
CI runner is typically a fresh environment every run.
Confirm the CI runner actually has Docker available and correctly configured before assuming a Testcontainers suite will "just work" the same as on a developer laptop:
# GitHub Actions: ubuntu-latest runners include Docker by default;
# nothing extra to configure for the common case
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./gradlew test
# GitLab CI: needs an explicit Docker-in-Docker service and TLS config
test:
image: eclipse-temurin:21-jdk
services:
- docker:24-dind
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
script:
- ./gradlew test
A CI platform that doesn't provide Docker by default (or provides it only via an explicitly-declared Docker-in-Docker service, as GitLab does) is the most common reason a Testcontainers suite passes locally and fails outright in CI with a connection-refused error before any test logic even runs.
Size CI runner resources for every container the suite starts concurrently, not just for the application under test:
# a runner/executor resource request sized for the app plus
# a Postgres and a Kafka broker running alongside it during tests
resources:
requests:
cpu: "2"
memory: "4Gi"
A runner sized only for running the application's own test process commonly OOMs or throttles severely once a real Postgres and a real Kafka broker are also competing for the same constrained CPU/memory — size explicitly for the concurrent container load, not just the test process.
Pin exact image tags for every container module, not latest —
an unpinned image tag means a suite's behavior (and its exact
startup time/readiness signal) can shift under a test run with no
corresponding code change:
new PostgreSQLContainer<>("postgres:16.4") // pinned, not "postgres:latest"
Prefer the language ecosystem's native Testcontainers module over a
generic container definition where one exists — the Postgres/
Kafka/Redis-specific modules ship pre-tuned wait strategies and
convenience accessors (getJdbcUrl(), getBootstrapServers()) that
a hand-rolled GenericContainer setup has to reimplement and is more
likely to get subtly wrong:
// prefer this
new PostgreSQLContainer<>("postgres:16.4");
// over hand-rolling the equivalent with GenericContainer, unless no
// maintained module exists for the target dependency
withReuse(true) for local developer-experience speedups, not
as a correctness assumption in CI, where runners are typically fresh
per run anyway.Symptom: A Testcontainers suite passes reliably on every
developer's laptop and fails immediately in CI with a connection-
refused or "cannot connect to the Docker daemon" error.
Fix: The CI runner either has no Docker daemon available at all,
or (on platforms like GitLab CI) needs an explicit Docker-in-Docker
service declared and DOCKER_HOST configured (step 6) — this isn't a
test-code bug, it's a CI environment gap. Confirm Docker's actual
availability and configuration on the specific CI platform before
debugging the test code itself.
Symptom: The same Testcontainers suite is markedly slower or intermittently times out only when run in CI, not locally. Fix: The CI runner is under-resourced for however many containers the suite starts concurrently (step 7) — a shared/constrained CI executor competing for CPU/memory across a real Postgres, a real Kafka broker, and the application's own test process is a common, easy-to-miss capacity gap. Size the runner's resource requests/limits explicitly for the concurrent container load, not just the application process.
Symptom: Tests pass individually but fail intermittently when run as a full suite, especially in a particular order. Fix: State from one test leaks into the next because the shared, class-scoped container's data was never reset between tests (step 3) — a common mistaken assumption that a shared container implies shared test isolation for free. Add an explicit reset step (truncate, transaction rollback, fresh topic/namespace per test) between tests.
Symptom: Two parallel CI jobs (or two developers running tests locally at the same time) both fail with a port-already-in-use error. Fix: A container port was hardcoded instead of read dynamically from the Testcontainers API (step 4) — Testcontainers maps to a random free host port specifically to avoid this; hardcoding a fixed port defeats that and breaks any parallel execution.
Symptom: Over weeks, a shared CI runner (or a developer's local
Docker daemon) accumulates dozens of leftover, no-longer-needed
containers from past test runs.
Fix: Testcontainers' Ryuk reaper (which cleans up containers after
the test process exits) either couldn't reach the Docker socket due to
a restrictive daemon/network policy, or was explicitly disabled
(TESTCONTAINERS_RYUK_DISABLED=true) without a replacement cleanup
mechanism. Confirm Ryuk can actually run in the target environment,
and if it genuinely must be disabled (some locked-down CI/security
policies block it), add an explicit scheduled cleanup step instead of
leaving orphaned containers to accumulate silently.
Scenario: order-service's repository layer is tested against a
real Postgres instead of a mocked JDBC driver, and its Kafka event
publisher is tested against a real Kafka broker instead of a mocked
producer — both need to run reliably in GitHub Actions CI, in parallel
across multiple test suites.
public abstract class AbstractIntegrationTest {
static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16.4")
.withDatabaseName("orders_test")
.withUsername("test")
.withPassword("test");
static final KafkaContainer kafka =
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.0"));
static {
postgres.start();
kafka.start();
}
@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
registry.add("spring.kafka.bootstrap-servers", kafka::getBootstrapServers);
}
}
class OrderRepositoryIntegrationTest extends AbstractIntegrationTest {
@BeforeEach
void resetSchema() {
jdbcTemplate.execute("TRUNCATE orders RESTART IDENTITY CASCADE");
}
@Test
void persistsAndReloadsAnOrder() {
Order saved = orderRepository.save(new Order("ORD-1", 4999));
assertThat(orderRepository.findById(saved.getId()).getAmountCents()).isEqualTo(4999);
}
}
class OrderEventPublisherIntegrationTest extends AbstractIntegrationTest {
@Test
void publishesOrderCreatedEvent() throws Exception {
orderEventPublisher.publish(new OrderCreatedEvent("ORD-2", 1999));
ConsumerRecords<String, String> records = testConsumer.poll(Duration.ofSeconds(5));
assertThat(records).hasSize(1);
}
}
CI (GitHub Actions, Docker available by default on ubuntu-latest, no
extra Docker-in-Docker configuration needed):
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { java-version: '21', distribution: 'temurin' }
- run: ./gradlew test
Both postgres and kafka are started once as static fields shared
across both test classes (extending the same AbstractIntegrationTest),
keeping suite startup cost to one Postgres and one Kafka container
total rather than one pair per test class; TRUNCATE before each
repository test keeps state isolated without restarting the container.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin iac-automation-skills14 skills in this plugin
/ansible-playbook-and-role-designDesigns and safely runs Ansible playbooks, roles, and inventories for configuration management, including idempotent task design, Ansible Vault for secrets, and choosing config-management (Ansible) versus provisioning (Terraform/CloudFormation) for a given change. Use when the user asks to "write an Ansible playbook/role for X," "structure an Ansible inventory across environments," "make this task idempotent," "encrypt secrets with Ansible Vault," "dry-run a playbook before production," or "should this be Ansible or Terraform."
/aws-cloudformation-templatesAuthors and safely operates AWS CloudFormation templates (YAML/JSON), including stacks, change sets, nested and cross-stack references, StackSets for multi-account/multi-region deployment, and drift detection. Use when the user asks to "write a CloudFormation template for X," "structure nested CloudFormation stacks," "deploy the same stack across AWS accounts/regions," "review a CloudFormation change set before applying," "detect/reconcile drift on a stack," or "migrate/compare CloudFormation with Terraform" for an AWS-native IaC choice.
/chef-puppet-saltstack-legacy-config-managementProvides a comparative overview of Chef, Puppet, and SaltStack for teams maintaining existing legacy configuration-management estates alongside or instead of Ansible — agent-based pull architecture, DSL/resource models (Chef recipes/cookbooks, Puppet manifests/catalogs, Salt states/pillars), and a candid assessment of where each still earns its keep in a modern enterprise versus where it's inertia. Use when the user asks to "maintain this existing Chef/Puppet/Salt estate," "write a Chef recipe/Puppet manifest/Salt state," "should we migrate off Puppet to Ansible," "why is our Puppet run applying a change we didn't expect," or "compare Chef vs. Puppet vs. SaltStack."
/feature-flag-configuration-launchdarkly-and-unleashDesigns and operates feature flag configuration with LaunchDarkly or Unleash, covering flag lifecycle (create, target, roll out, retire), kill-switch patterns for fast incident mitigation without a redeploy, targeting/segmentation rules, and cleaning up stale flag debt before it accumulates into unreadable conditional logic. Use when the user asks to "add a feature flag for X," "set up a kill switch for this feature," "configure LaunchDarkly/Unleash targeting rules," "roll out a flag gradually by percentage," "clean up old feature flags," or "should we use LaunchDarkly or self-host Unleash."
/gremlin-chaos-engineering-configurationConfigures Gremlin-specific chaos engineering attacks — installing the Gremlin agent/sidecar, defining resource, state, and network attacks via the Gremlin API/CLI or Terraform provider, scoping blast radius with targets and halt conditions, and running Gremlin Scenarios for multi-step failure sequences. Use when the user asks to "configure a Gremlin attack," "set up the Gremlin agent in Kubernetes/EC2," "write a Gremlin Scenario," "scope a Gremlin blast radius/halt condition," or "use Gremlin to test our failover." For the general chaos-engineering principles (steady-state hypothesis, game days, tool-agnostic blast radius scoping) this configures against, use the SRE chaos-engineering-and-resilience-testing skill instead.
/infrastructure-post-deployment-validation-and-smoke-testingVerifies that a Terraform apply, CloudFormation deploy, or Ansible playbook run actually produced working infrastructure — resource existence and configuration checks, connectivity/health smoke tests, and automated post-apply assertions — rather than treating a clean "apply succeeded" exit code as proof the system works. Use when the user asks to "verify this deployment actually worked," "write a smoke test after terraform apply/cloudformation deploy," "check the resource is actually reachable/configured correctly post-deploy," "add post-deployment validation to the pipeline," or "our apply succeeded but the service is down."
Other plugins' skills for the same job