Rethinking integration testing with Testcontainers

A presentation at Devoxx UK in May 2022 in London, UK by Oleg Šelajev

Slide 1

Slide 1

Rethinking integration testing with Testcontainers

Slide 2

Slide 2

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

Slide 3

Slide 3

➡ 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

Slide 4

Slide 4

📖 https://bit.ly/devoxx-uk-2022-TC-lab

Slide 5

Slide 5

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

Slide 6

Slide 6

Oleg Šelajev Developer relations Kevin Wittek Software engineer Piotr Przybył Software engineer / gardener

Slide 7

Slide 7

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

Slide 8

Slide 8

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

Slide 9

Slide 9

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

Slide 10

Slide 10

Slide 11

Slide 11

• Process isolation • FileSystem, Network • CPU/memory limits

Slide 12

Slide 12

• 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 😅

Slide 13

Slide 13

testcontainers.org

Slide 14

Slide 14

Slide 15

Slide 15

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

Slide 16

Slide 16

During After

Slide 17

Slide 17

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

Slide 18

Slide 18

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

Slide 19

Slide 19

Downloads

Slide 20

Slide 20

Unique IPs

Slide 21

Slide 21

What to do if you have no Docker?

Slide 22

Slide 22

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

Slide 23

Slide 23

Testcontainers Cloud testcontainers.cloud

Slide 24

Slide 24

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

Slide 25

Slide 25

Resources — github.com/testcontainers/testcontainers-java — www.testcontainers.org

Slide 26

Slide 26

Let’s get started ! — github.com/testcontainers/workshop

Slide 27

Slide 27

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

Slide 28

Slide 28

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

Slide 29

Slide 29

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

Slide 30

Slide 30

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

Slide 31

Slide 31

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

Slide 32

Slide 32

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

Slide 33

Slide 33

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

Slide 34

Slide 34

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

Slide 35

Slide 35

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 — ✅