Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
DBアクセスを伴う自動テストを書くときのプラクティス/Practices When Writ...
Search
yukana
September 26, 2024
Programming
110
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DBアクセスを伴う自動テストを書くときのプラクティス/Practices When Writing Automated Tests with DB Access
yukana
September 26, 2024
More Decks by yukana
See All by yukana
Gradleよくわからんから脱する/Gradle Getting Started
yukana
0
99
Nice to haveな気づきに向き合う / Facing Nice to Have Awareness
yukana
1
68
ユニットテスト実行を 45% 高速化した Repository テスト戦略 / Repository Test Strategy Speeds up Unit Test
yukana
6
2.1k
Other Decks in Programming
See All in Programming
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
590
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.1k
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
3.6k
AWSに止められる覚悟してますか?
morizo_1984
2
540
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.5k
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
400
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
500
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
410
We Are The Robots
honzajavorek
0
400
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Into the Great Unknown - MozCon
thekraken
41
2.8k
Test your architecture with Archunit
thirion
2
2.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
570
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Abbi's Birthday
coloredviolet
4
10k
Transcript
DBアクセスを伴う結合テストを 書くときのプラクティス BABY JOB株式会社 第8回社内LT 2024/09/26 西牧 佑哉
大事なこと • テストの環境を本番とできるだけ合わせる ◦ テストの環境が本番と違っていると結果を信頼しづらくなる 2
テスト用DBのスキーマファイルを手動で管理しない 3
想定しているケース • DBの変更履歴をマイグレーションファイルで管理している • テスト用DBのセットアップのためにスキーマファイルを用意している 4
手動管理のデメリット • マイグレーションファイルの内容に合わせて変更するのが面倒 • ミスがあっても気づきづらい 5
どうするか? • スキーマファイルを用意しない ◦ テスト実行前にマイグレーションする • 自動生成する(ダンプする) ◦ マイグレーションに時間がかかる場合に有効 ◦
マイグレーション後に必ずスキーマファイルが生成/更新される仕組みにしておくと常に最新 に保てる 6
インメモリDBを使用しない 7
想定しているケース • ローカルではDockerで立てたDBを使用して開発している • テストではSQLiteやH2のようなインメモリDBを使用している 8
インメモリDBを使用するメリデメ • メリット ◦ セットアップが簡単 • デメリット ◦ 本番環境やローカルでの動作確認時と異なる挙動をする可能性がある ▪
つまり、テスト結果を信頼しづらい 9
どうするか? • ローカルや本番と同じDBを使用する ◦ Testcontainersを使うと簡単にできる! 10
テストの準備、実行、検証を 同一トランザクション上で行わない 11
想定しているケース • テスト実行時の挙動 1. トランザクション開始 a. 準備(テスト用のデータのインサート) b. 実行(例:何らかの保存処理) c.
検証(例:DBからデータを取得して保存した内容の確認) 2. ロールバック 12
同一トランザクションで行うメリデメ • メリット ◦ テスト終了後にロールバックしてテストデータの後始末ができる • デメリット ◦ 本番では準備、実行、検証が同一トランザクション上で行われない ▪
つまり、テストと本番でやっていることが異なる 13
どうするか? • 各フェーズごとにトランザクションを分ける • テストデータの後始末はテスト前に行うようにする ◦ 他のテストに依存せずに、確実にクリーンアップできる 14
参考 • 単体テストの考え方/使い方 15
まとめ • テスト用DBのスキーマファイルを手動で管理しない • インメモリDBを使用しない • テストの準備、実行、検証を同一トランザクション上で行わない 16