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
150
ory/dockertestでテストの下準備をなるべく減らす
Asakusa.go #1
shuymn
October 13, 2023
Tweet
Share
More Decks by shuymn
See All by shuymn
hojiroLT 9
shuymn
2
68
hojiroLT 7
shuymn
0
790
hojiroLT 8
shuymn
0
240
Featured
See All Featured
Making Projects Easy
brettharned
109
5.5k
Building a Scalable Design System with Sketch
lauravandoore
457
32k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
How to train your dragon (web standard)
notwaldorf
74
5.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
Writing Fast Ruby
sferik
622
60k
Facilitating Awesome Meetings
lara
43
5.6k
Into the Great Unknown - MozCon
thekraken
14
1k
YesSQL, Process and Tooling at Scale
rocio
165
13k
Mobile First: as difficult as doing things right
swwweet
217
8.6k
Faster Mobile Websites
deanohume
300
30k
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つのテストケースを実行する場合、
コンテナを起動するまでのオーバーヘッドが気になる
おわり • 質疑応答