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
550
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DBを使ったテストで詰まった点とその対策
Rustを使ったWebアプリケーションでのDBを用いたテストに関する発表です
sontixyou
September 11, 2024
More Decks by sontixyou
See All by sontixyou
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
7
4.1k
Claude Codeと駆け抜ける 情報収集と実践録
sontixyou
2
2k
開発メンバーが語るFindy Conferenceの裏側とこれから
sontixyou
2
1.7k
Featured
See All Featured
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
400
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
640
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
Ethics towards AI in product and experience design
skipperchong
2
330
Git: the NoSQL Database
bkeepers
PRO
432
67k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
350
Measuring & Analyzing Core Web Vitals
bluesmoon
9
940
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
230
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
420
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 はじめました!!