Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Integration testing with TestContainers-Go
Search
Nikolay Kuznetsov
January 23, 2020
Programming
0
260
Integration testing with TestContainers-Go
Go Days 2020 - Berlin
Nikolay Kuznetsov
January 23, 2020
Tweet
Share
More Decks by Nikolay Kuznetsov
See All by Nikolay Kuznetsov
Integration testing with TestСontainers-Go
nikolayk812
0
130
Integration testing with TestСontainers and JUnit 5
nikolayk812
0
170
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
350
Integration and end-to-end testing with TestСontainers
nikolayk812
0
120
Integration and end-to-end testing with TestСontainers
nikolayk812
0
29
Other Decks in Programming
See All in Programming
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
330
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
130
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
430
How Software Deployment tools have changed in the past 20 years
geshan
0
29k
Microservices rules: What good looks like
cer
PRO
0
1.3k
【Streamlit x Snowflake】データ基盤からアプリ開発・AI活用まで、すべてをSnowflake内で実現
ayumu_yamaguchi
1
120
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
6
2.2k
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
310
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
390
配送計画の均等化機能を提供する取り組みについて(⽩⾦鉱業 Meetup Vol.21@六本⽊(数理最適化編))
izu_nori
0
150
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
tparseでgo testの出力を見やすくする
utgwkk
1
210
Featured
See All Featured
Embracing the Ebb and Flow
colly
88
4.9k
What's in a price? How to price your products and services
michaelherold
246
12k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.7k
Facilitating Awesome Meetings
lara
57
6.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Documentation Writing (for coders)
carmenintech
76
5.2k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Navigating Team Friction
lara
191
16k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
BBQ
matthewcrist
89
9.9k
Into the Great Unknown - MozCon
thekraken
40
2.2k
Transcript
Integration testing with TestСontainers-Go Erdem Toraman Nikolay Kuznetsov GoDays -
Berlin 23 Jan 2020
About us • Go developers at Zalando in Helsinki •
Project with ~20 microservices in Go • User perspective of TestContainers-Go library
2 unit tests passed, 0 integration tests
Basic integration test
Getting a database for testing • Local database • In-memory
mock • Docker!
Docker advantages • 100% compatible databases • Same version as
production • Empty DB state
Integration testing with Docker
Easy-peasy! docker run -d -p 5432:5432 postgres:12.1
• Host port conflicts • Not ready сontainer / service
• Resource leak (the container keeps running) • Stale data (if reusing the same container) • Starting mechanism both for CI and a local machine What could go wrong?
Solving some issues
None
Docker API docs.docker.com/engine/api/latest
Exec example
None
TestContainers flavors
TestContainers-Go github.com/testcontainers/testcontainers-go • Docker Go client under the hood •
Host port randomization • Containers clean up at the test shutdown • Readiness waiting strategies
As simple as pgContainer, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ ContainerRequest: tc.ContainerRequest{
Image: "postgres:12.1", ExposedPorts: []string{"5432/tcp"}, }, })
Host port randomization • API to get a host port:
• Prevents port conflicts • Enables parallel builds port, err := pgContainer.MappedPort(ctx, "5432/tcp")
Containers cleanup: Ryuk github.com/testcontainers/moby-ryuk • Ryuk kills containers (networks, volumes)
by labels • TC assigns labels to started containers • TC keeps a connection to Ryuk open
Waiting strategies • Host port • HTTP • Logs •
Custom • Multi
Host port waiting strategy • Default (customizable) timeout is 60
seconds • Impl checks both from outside and inside container tc.ContainerRequest{ Image: "postgres:12.1", ExposedPorts: []string{"5432/tcp"}, WaitingFor: wait.ForListeningPort("5432/tcp"), },
HTTP waiting strategy WaitingFor: wait.ForHTTP("/health"). WithPort("8080/tcp"). WithStatusCodeMatcher( func(status int) bool
{ return status == http.StatusOK }),
Demo
Demo I: integration testing User Repository github.com/erdemtoraman/godays-testcontainers-demo • Testing interaction
with a database • Create and Get users from Postgres
What happened? • Ran a Postgres container • Mapped a
random port to the host machine • Ran tests against it • Cleaned up the containers
Demo II: end-to-end testing User Service github.com/erdemtoraman/godays-testcontainers-demo Ticket Service Docker
Network Tests HTTP
Takeaways • testcontainers.org • Balance between flexibility, speed and features
• Great for integration tests • Possible to use for end-to-end tests
Thank you! @erdem_toraman, @nikolayk812 erdemtoraman