agentleFS
MathiasPaulenkoMathiasPaulenko/ai-toolkit/skills/testcontainers/SKILL.md

testcontainers

Skill2 starsChanged 4 months agoMITView on GitHub

Integration testing with Testcontainers: PostgreSQL, Redis, Kafka, RabbitMQ, localstack. Spin up real dependencies in Docker for reliable integration tests.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Testcontainers

Integration testing with real dependencies via Docker containers.

1. Why Testcontainers

ProblemTestcontainers Solution
In-memory DB ≠ productionReal PostgreSQL, Redis, MongoDB in Docker
H2 dialect differencesSame engine as production
Kafka mocks are incompleteReal Kafka broker + topics
AWS testing is hardLocalstack S3, SQS, DynamoDB
Test isolationFresh container per test class / JVM

2. Quick Start (Java + JUnit 5)

<!-- pom.xml -->
<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>1.19.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>postgresql</artifactId>
  <version>1.19.0</version>
  <scope>test</scope>
</dependency>
@Testcontainers
public class OrderRepositoryTest {

  @Container
  static PostgreSQLContainer<?> postgres =
    new PostgreSQLContainer<>("postgres:15-alpine")
      .withDatabaseName("orders")
      .withUsername("test")
      .withPassword("test");

  @DynamicPropertySource
  static void props(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgres::getJdbcUrl);
    registry.add("spring.datasource.username", postgres::getUsername);
    registry.add("spring.datasource.password", postgres::getPassword);
  }

  @Autowired OrderRepository repo;

  @Test
  void shouldSaveAndFindOrder() {
    Order order = new Order("ORD-001", BigDecimal.valueOf(99.99));
    repo.save(order);
    assertThat(repo.findById("ORD-001")).isPresent();
  }
}

3. Container Types & Patterns

PostgreSQL

@Container
static PostgreSQLContainer<?> pg = new PostgreSQLContainer<>("postgres:15")
  .withInitScript("schema.sql");  // Runs on startup

Redis

@Container
static GenericContainer<?> redis = new GenericContainer<>("redis:7-alpine")
  .withExposedPorts(6379);

@Bean
@Primary
RedisConnectionFactory redisFactory() {
  return LettuceConnectionFactory.builder()
    .host(redis.getHost())
    .port(redis.getMappedPort(6379))
    .build();
}

Kafka

@Container
static KafkaContainer kafka = new KafkaContainer(
  DockerImageName.parse("confluentinc/cp-kafka:7.5.0")
);

// Producer test
KafkaProducer<String, String> producer = new KafkaProducer<>(
  Map.of(
    ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers(),
    ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(),
    ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()
  )
);

Localstack (AWS)

@Container
static LocalStackContainer localstack = new LocalStackContainer(
  DockerImageName.parse("localstack/localstack:2.3")
).withServices(S3, SQS, DYNAMODB);

S3Client s3 = S3Client.builder()
  .endpointOverride(localstack.getEndpointOverride(S3))
  .credentialsProvider(StaticCredentialsProvider.create(
    AwsBasicCredentials.create("test", "test")
  ))
  .region(Region.US_EAST_1)
  .build();

4. Python Testcontainers

# pytest + testcontainers
from testcontainers.postgres import PostgresContainer
import psycopg2

def test_postgres():
    with PostgresContainer("postgres:15-alpine") as postgres:
        conn = psycopg2.connect(postgres.get_connection_url())
        cur = conn.cursor()
        cur.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)")
        cur.execute("INSERT INTO users (name) VALUES ('Alice')")
        conn.commit()
        cur.execute("SELECT * FROM users")
        assert cur.fetchone() == (1, "Alice")

5. Lifecycle Optimization

StrategyUse CaseSpeed
Per test classDefault, good isolationMedium
Singleton containerShared across suiteFast
@RestartPerTestMaximum isolationSlow
// Singleton (faster)
@Testcontainers
public class SharedDbTest {
  @Container
  static PostgreSQLContainer<?> pg = new PostgreSQLContainer<>("postgres:15");
  // static = one container for all tests in class
}

6. Docker Compose

@Container
static DockerComposeContainer<?> compose =
  new DockerComposeContainer<>(new File("docker-compose.test.yml"))
    .withExposedService("app", 8080)
    .withExposedService("db", 5432);

String appHost = compose.getServiceHost("app", 8080);
int appPort = compose.getServicePort("app", 8080);

7. CI/CD Requirements

  • Docker daemon available (GitHub Actions ubuntu-latest has it)
  • Ryuk container cleanup enabled by default
  • Mount /var/run/docker.sock if running in Docker-in-Docker
# .github/workflows/test.yml
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with: { java-version: '21', distribution: 'temurin' }
      - run: mvn test -Dtest='*Testcontainers*'

8. Anti-Patterns

Anti-PatternFix
Using H2 in production, PG in testsUse same DB in tests
@Container on non-static fieldContainer restarts per test method
Hardcoding portsUse getMappedPort()
No schema initUse .withInitScript() or Flyway
Ignoring container logsEnable log streaming for debugging

9. Related Resources

  • Skills: sql-server, postgresql (if added), flask-api
  • Prompts: generate-integration-test-suite (QA)
  • Agents: test-architect

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.