Rethinking integration testing with Testcontainers

πŸ›  git IDE Docker (including Docker compose for some steps) Build tool: Maven / Gradle

➑ github.com/testcontainers/workshop docker pull postgres:14-alpine docker pull redis:6-alpine docker pull openjdk:8-jre-alpine fl docker pull con uentinc/cp-kafka:5.4.3

πŸ“– https://bit.ly/devoxx-uk-2022-TC-lab

Agenda Intro & theory β€” 10 minutes Setup the project β€” 15 minutes Adding the rst tests β€” 10 minutes Your rst Testcontainers integration β€” 15 minutes Redis tests & Redis Generic container β€” 20 minutes Kafka tests β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Oleg Ε elajev Developer relations Kevin Wittek Software engineer Piotr PrzybyΕ‚ Software engineer / gardener

e2e Integration tests Unit tests By Unknown author - U.S. Army Photo, Public Domain, https://commons.wikimedia.org/w/index.php?curid=55124

Microservices Testing Honeycomb https://labs.spotify.com/2018/01/11/testing-of-microservices/

https://martinfowler.com/bliki/IntegrationTest.html

β€’ Process isolation β€’ FileSystem, Network β€’ CPU/memory limits

β€’ Con guring multiple services simultaneously β€’ Flexibility in creating test data or use-cases β€’ Separation of production & test environments β€’ Dynamic con guration of external mappings β€’ Parallel deployments fi fi πŸ˜…

testcontainers.org

System checks Checking the system… βœ” Docker version should be at least 1.6.0 βœ” Docker environment should have more than 2GB free disk space βœ” File should be mountable β„ΉοΈŽ βœ” A port exposed by a docker container should be accessible

During After

The problem Extra mile Port conflicts Port randomization Hard-coded scenarios Data-driven tests Slow tests Parallelization

We think it’s a useful default option for creating a reliable environment for running tests. … Our teams have consistently found this library of programmable, lightweight and disposable containers to make functional tests more reliable. https://www.thoughtworks.com/en-us/radar/languages-and-frameworks/testcontainers

Downloads

Unique IPs

What to do if you have no Docker?

Docker environments https://www.atomicjar.com/2021/10/docker-on-windows-and-macos

Testcontainers Cloud testcontainers.cloud

Model: record Rating (String talkId, Integer value) {}

Resources β€” github.com/testcontainers/testcontainers-java β€” www.testcontainers.org

Let’s get started ! β€” github.com/testcontainers/workshop

Agenda Intro & theory β€” βœ… Setup the project: Steps 1 & 2 β€” 15 minutes Adding the rst tests β€” 10 minutes Your rst Testcontainers integration β€” 15 minutes Redis tests & Redis Generic container β€” 20 minutes Kafka tests β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min github.com/ testcontainers/ workshop

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests: Step 3 β€” 10 minutes Your rst Testcontainers integration β€” 15 minutes Redis tests & Redis Generic container β€” 20 minutes Kafka tests β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration: Step 4 β€” 15 minutes Redis tests & Redis Generic container β€” 20 minutes Kafka tests β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container: Steps 5 & 6 β€” 20 minutes Kafka tests β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container β€” βœ… Kafka tests: Step 7 β€” 15 minutes Testcontainers without the framework support β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container β€” βœ… Kafka tests β€” βœ… Testcontainers without the framework support: Step 8 β€” 20 min Database initialization setup β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container β€” βœ… Kafka tests β€” βœ… Testcontainers without the framework support β€” βœ… Database initialization setup: Step 9 β€” 20 min fi fi Migrating Docker-compose to Testcontainers β€” 20 min

Agenda Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container β€” βœ… Kafka tests β€” βœ… Testcontainers without the framework support β€” βœ… Database initialization setup β€” βœ… fi fi Migrating Docker-compose to Testcontainers: Step 10 β€” 20 min

Congrats! Intro & theory β€” βœ… Setup the project β€” βœ… Adding the rst tests β€” βœ… Your rst Testcontainers integration β€” βœ… Redis tests & Redis Generic container β€” βœ… Kafka tests β€” βœ… Testcontainers without the framework support β€” βœ… Database initialization setup β€” βœ… fi fi Migrating Docker-compose to Testcontainers β€” βœ