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
DBを使ったテストで詰まった点とその対策
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sontixyou
September 11, 2024
500
0
Share
DBを使ったテストで詰まった点とその対策
Rustを使ったWebアプリケーションでのDBを用いたテストに関する発表です
sontixyou
September 11, 2024
More Decks by sontixyou
See All by sontixyou
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.2k
Claude Codeと駆け抜ける 情報収集と実践録
sontixyou
2
1.9k
開発メンバーが語るFindy Conferenceの裏側とこれから
sontixyou
2
1.5k
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
220
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
110
Utilizing Notion as your number one productivity tool
mfonobong
4
290
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
220
Why Our Code Smells
bkeepers
PRO
340
58k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.3k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
97
Transcript
2024/09/11 RustのWebアプリ開発 LT道場 〜テスト編〜 DBを使ったテストで詰まった点とその対策 01 1
02 2 西村 憲悟 [にしむら けんご] ファインディ株式会社 / バックエンド @sontixyou
• Webアプリケーションのテストで直面した課題 • 問題に対しての解決アプローチ • まとめ 今日話すこと 03 3
今日話さないこと 04 • テストデータの作成を楽にする方法 • どこの月見バーガーがうまいのか 4
• 実行を短時間で終わらせたい • テストケース間で干渉しないようにテス トしたい テストに対してのモチベーション 05 5
アプリケーションの構成 06 6 アプリケーション DB ORM: sea-orm https://github.com/SeaQL/sea-orm
アプリケーションコード 07 7 DBコネクションしてから postテーブルにレコードを作成 DBコネクションしてから
postテーブルにあるレコードを更新
テストコード 08 8
各テストがひとつのDBを使う状態で cargo testするとどうなるのか? テストの問題点 09 9
• 複数のテストが同じDBにデータを読み書きすると、 テスト同士で影響が出る ◦ テストが失敗したり、flakyになる テストの問題点 10 10
ユニーク制約ぐぬぬ DBのユニーク制約にひっかかり、自分が作りたいデータ を作成できない テストの問題点 11 11
• △ 実行を短時間で終わらせたい • ✗ テストケース間で干渉する テストの問題点 12 12
cargo testをすると並列でテストが実行されるため https://doc.rust-lang.org/book/ch11-02-running-tests.html#running-tests-in-parallel-or-consecutively DBへCRUDする場合での難しさ 13 13
• テストケースごとにDBを分ける • 並列数を1にする ◦ cargo testのオプションで可能 • モックDBを使う ◦
sea-ormでサポートされている 対応方法候補 14 14
• テストケースごとにDBを分ける • 並列数を1にする • モックDBを使う 対応方法候補 15 15
テスト実行前にDBを作成。 実行後にDBを削除する。 ↓ test_contextを使用することで実現可能になる。 https://docs.rs/test-context/latest/test_context/ テストケースごとにDBを分ける 準備編 16 16
テストケースごとにDBを分ける 実装編 17 17
テストケースごとにDBを分ける 実装編 18 18 setupとteardown関数を呼び出す下準備
テストケースごとにDBを分ける 実装編 19 19 テストに対してのモチベーション • ✅ テスト実行を短時間で終わる • ✅ テストケース間で干渉しない
テストケースごとにDBを分ける 実装編 20 20 実装の詳細はこちらを見てほしい🦀 https://github.com/sontixyou/sea_orm_practice_db2024
まとめ 21 21 • 並列でテスト実行をするときはDBの接続先に気 をつけよう • sea-ormを使う際は一工夫が必要 ◦ 自前実装が手間ならsqlxを使うのも手
宣伝 開催日時: 10月 8日 (火曜日) 19:30~ ↓詳細はこちら↓ 22 22 🦀Rust地域コミュニティ🦀
Osaki.rs はじめました!!