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
Integration testing with TestСontainers-Go
Search
Nikolay Kuznetsov
June 18, 2020
Programming
0
130
Integration testing with TestСontainers-Go
GopherCon Europe 2020 - Lightning talk
Nikolay Kuznetsov
June 18, 2020
Tweet
Share
More Decks by Nikolay Kuznetsov
See All by Nikolay Kuznetsov
Integration testing with TestContainers-Go
nikolayk812
0
260
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
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
620
CSC307 Lecture 03
javiergs
PRO
1
490
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
140
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
200
なるべく楽してバックエンドに型をつけたい!(楽とは言ってない)
hibiki_cube
0
140
組織で育むオブザーバビリティ
ryota_hnk
0
180
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6.1k
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
180
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
220
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
Featured
See All Featured
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
170
We Are The Robots
honzajavorek
0
160
Scaling GitHub
holman
464
140k
WCS-LA-2024
lcolladotor
0
450
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
170
Practical Orchestrator
shlominoach
191
11k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
50k
The Curious Case for Waylosing
cassininazir
0
240
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
190
Game over? The fight for quality and originality in the time of robots
wayneb77
1
120
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
Transcript
Integration testing with TestСontainers-Go Nikolay Kuznetsov GopherCon Europe 18 Jun
2020 @nikolayk812
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) What could go wrong?
Solving some issues
None
Docker API docs.docker.com/engine/api/latest
Exec example
None
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 • Ryuk acts when the connection is terminated
Waiting strategies • Host port • HTTP • SQL •
Logs • Custom • Multi
Host port waiting strategy • Impl checks both from outside
and inside container • Default (customizable) timeout is 60 seconds 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 }),
Takeaways • testcontainers.org • Balance between flexibility, speed and features
• Great for integration tests • Possible to use for end-to-end tests
Thank you! @nikolayk812 nikolayk812