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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
100
Nice to haveな気づきに向き合う / Facing Nice to Have Awareness
yukana
1
70
ユニットテスト実行を 45% 高速化した Repository テスト戦略 / Repository Test Strategy Speeds up Unit Test
yukana
6
2.1k
Other Decks in Programming
See All in Programming
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
300
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
360
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
190
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
150
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
8.8k
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.4k
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
330
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
150
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
160
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
150
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.7k
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
290
How to Think Like a Performance Engineer
csswizardry
28
2.9k
How to Talk to Developers About Accessibility
jct
2
610
Test your architecture with Archunit
thirion
2
2.5k
Mind Mapping
helmedeiros
1
430
Accessibility Awareness
sabderemane
1
290
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
The Curious Case for Waylosing
cassininazir
1
610
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
450
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
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