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
250
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ory/dockertestでテストの下準備をなるべく減らす
Asakusa.go #1
shuymn
October 13, 2023
More Decks by shuymn
See All by shuymn
hojiroLT 9
shuymn
2
93
hojiroLT 7
shuymn
0
1k
hojiroLT 8
shuymn
0
270
Featured
See All Featured
How to build a perfect <img>
jonoalderson
1
6k
Prompt Engineering for Job Search
mfonobong
0
440
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
510
How to make the Groovebox
asonas
2
2.4k
How STYLIGHT went responsive
nonsquared
100
6.3k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
490
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.7k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
400
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つのテストケースを実行する場合、
コンテナを起動するまでのオーバーヘッドが気になる
おわり • 質疑応答