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
ory/dockertestでテストの下準備をなるべく減らす
Search
shuymn
October 13, 2023
0
240
ory/dockertestでテストの下準備をなるべく減らす
Asakusa.go #1
shuymn
October 13, 2023
Tweet
Share
More Decks by shuymn
See All by shuymn
hojiroLT 9
shuymn
2
84
hojiroLT 7
shuymn
0
1k
hojiroLT 8
shuymn
0
260
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Believing is Seeing
oripsolob
1
60
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
Why Our Code Smells
bkeepers
PRO
340
58k
Designing for humans not robots
tammielis
254
26k
30 Presentation Tips
portentint
PRO
1
230
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
230
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
Design in an AI World
tapps
0
150
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
310
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Transcript
ory/dockertestで テストの下準備を なるべく減らす話 @shuymn / Asakusa.go #1
目次 • 前提 & なぜ下準備を減らしたいのか • ory/dockertest の紹介 • ory/dockertest
の実践的な使い方(実際のコードを見ながら) • 今後の課題や展望 • 質疑応答
なぜ下準備を減らしたいのか • 複数リポジトリ(サービス)を横断して開発することがある • 各サービスでテストの実行方法、テスト前にやっておくことが微妙に違う ◦ docker compose up ◦
DBのマイグレーション(DBが共通だとより大変) ◦ など • 修正するよりもテストを動かすほうが時間がかかって苦痛 • とりあえず go test ./... か make test だけで完結してほしい
ory/dockertestを使う理由 • テスト中でDockerコンテナを起動するので下準備が減らせる ◦ ポートの衝突も防げる ◦ Goで書ける範囲の下準備はコードで実行できる • テスト中にDBなどのデータの衝突を防ぎやすくなる
(前提)Goのテストが実行される単位 • go test ./... • package X と package
Y は並列 • x1_test.go と x2_test.go は直列 • TestX1_Foo と TestX1_Bar は直列 • (t.Parallelを使うと変わる) ◦ Go言語でのテストの並列化 〜t.Parallel()メソッドを理解する〜
(前提)Goのテストが実行される単位 • package X と package Y は並列 • データが競合する
• go test -p1 ./... は使いたくない • DATA-DOG/go-txdb を使う → トランザクションが使えない* RDBでしか使えない
ory/dockertestでデータの衝突を防ぐ • TestMain関数を利用 • Dockerコンテナ単位でデータを分離する • package単位でテストは直列なので 衝突は考えなくて良くなる • docker
compose でやるのは大変
ory/dockertest の実践的な使い方 • 実際のコードで説明 • shuymn-sandbox/dockertest-example
まとめ • dockertest.Pool を使い回せるような設計にする • コンテナの終了処理をする関数を返してTestMainで後処理をする • Dockerfileを指定する場合はコンテナ名が衝突しないようにする必要がある
今後の展望や課題 • テストデータの追加ってみなさんどうやってますか? • RDBだと go-testfixtures/testfixtures があるが Firestore等のNoSQLは未対応 • VSCode等から1つのテストケースを実行する場合、
コンテナを起動するまでのオーバーヘッドが気になる
おわり • 質疑応答