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 TestContainers-Go
Search
Nikolay Kuznetsov
January 23, 2020
Programming
0
220
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
120
Integration testing with TestСontainers and JUnit 5
nikolayk812
0
160
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
290
Integration and end-to-end testing with TestСontainers
nikolayk812
0
96
Integration and end-to-end testing with TestСontainers
nikolayk812
0
18
Other Decks in Programming
See All in Programming
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
210
Remix on Hono on Cloudflare Workers
yusukebe
1
290
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
Outline View in SwiftUI
1024jp
1
330
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
C++でシェーダを書く
fadis
6
4.1k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Statistics for Hackers
jakevdp
796
220k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
It's Worth the Effort
3n
183
27k
Site-Speed That Sticks
csswizardry
0
25
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Unsuck your backbone
ammeep
668
57k
The Invisible Side of Design
smashingmag
298
50k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
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