Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TestContainers-Go
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Erdem Toraman
January 23, 2020
Programming
0
94
TestContainers-Go
Erdem Toraman
January 23, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
Package Management Learnings from Homebrew
mikemcquaid
0
220
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
180
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
480
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
Fragmented Architectures
denyspoltorak
0
150
CSC307 Lecture 03
javiergs
PRO
1
490
Grafana:建立系統全知視角的捷徑
blueswen
0
330
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
610
Oxlintはいいぞ
yug1224
5
1.3k
Oxlint JS plugins
kazupon
1
890
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
320
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.3k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
910
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
Documentation Writing (for coders)
carmenintech
77
5.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
92
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
64
Faster Mobile Websites
deanohume
310
31k
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 the
port to a random host machine port • Ran tests against it • Cleaned up the container go test ./...
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