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
240
Integration testing with TestСontainers and JUnit 5
nikolayk812
0
160
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
330
Integration and end-to-end testing with TestСontainers
nikolayk812
0
110
Integration and end-to-end testing with TestСontainers
nikolayk812
0
23
Other Decks in Programming
See All in Programming
Benchmark
sysong
0
130
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
16
4k
從零到一:搭建你的第一個 Observability 平台
blueswen
1
870
Effect の双対、Coeffect
yukikurage
5
1.4k
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
Spring gRPC で始める gRPC 入門 / Introduction to gRPC with Spring gRPC
mackey0225
2
480
Using AI Tools Around Software Development
inouehi
0
1.2k
インターフェース設計のコツとツボ
togishima
2
700
Perlで痩せる
yuukis
1
680
Gleamという選択肢
comamoca
6
690
カクヨムAndroidアプリのリブート
numeroanddev
0
410
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
110
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
68
11k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Code Review Best Practice
trishagee
68
18k
Building Adaptive Systems
keathley
43
2.6k
Building an army of robots
kneath
306
45k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
480
Art, The Web, and Tiny UX
lynnandtonic
299
21k
GitHub's CSS Performance
jonrohan
1031
460k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Adopting Sorbet at Scale
ufuk
77
9.4k
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