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
DRY & 型安全にテスト用structを初期化しよう
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Kanji Yomoda
October 10, 2020
Programming
1
690
DRY & 型安全にテスト用structを初期化しよう
Go Conference 2020 Autumnの登壇資料です。
YoutubeのURLは
https://www.youtube.com/watch?v=DkWUh4oR0ac
になります。
Kanji Yomoda
October 10, 2020
Tweet
Share
More Decks by Kanji Yomoda
See All by Kanji Yomoda
Elasticstack Terraform Providerの最近のアップデート
kyomo
0
100
Goで作る全文検索エンジンライブラリ
kyomo
0
1.2k
Terraform Provider開発のノウハウ
kyomo
1
270
Paper reading - ROSE: Robust Caches for Amazon Product Search
kyomo
0
62
Elasticsearch Aggregations
kyomo
0
81
AlloyDB overview
kyomo
0
110
Unsupervised synonym Extraction
kyomo
0
320
pubsub_cli - CLI Tool for Cloud Pub/Sub
kyomo
0
130
DatastoreからSpannerへのゼロダウンタイム移行
kyomo
0
120
Other Decks in Programming
See All in Programming
AI時代の認知負荷との向き合い方
optfit
0
130
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
180
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
15
4.9k
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
370
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.4k
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
260
AtCoder Conference 2025
shindannin
0
1k
Architectural Extensions
denyspoltorak
0
260
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
360
CSC307 Lecture 06
javiergs
PRO
0
680
AgentCoreとHuman in the Loop
har1101
5
210
Vibe codingでおすすめの言語と開発手法
uyuki234
0
210
Featured
See All Featured
So, you think you're a good person
axbom
PRO
2
1.9k
Believing is Seeing
oripsolob
1
48
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
How to make the Groovebox
asonas
2
1.9k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
400
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
270
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
50
Designing for humans not robots
tammielis
254
26k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
170
Transcript
DRY & 型安全にテスト用structを初期化し よう Kanji Yomoda Go Conference 2020 Autumn
自己紹介 四方田 貫児 / Kanji Yomoda エムスリー 株式会社 @k__yomo
テストを書くのは大変・辛い - テストしやすいコード - 依存の解決 - テスト用のデータの準備 - and so
on...
- テスト用データ生成のライブラリのpros/cons - 既存ライブラリの課題 - 課題解決のために開発したライブラリの紹介 - まとめ 概要
テスト用データ生成ライブラリ testfixtures factory-go
testfixtures Go YAML YAMLからDBのレコードを作るライブラリ
Pros - シンプル - YAMLの可読性が高い Cons - テスト時の入力(引数)用のデータやmockの返り値としては使えない - 値が柔軟な設定・使い回しが出来ない
- RDBにしか使えない testfixtures
柔軟なstruct初期化ライブラリ factory-go
Pros - 導入が簡単 - 値の設定が柔軟(再帰的な初期化なども可能) Cons - Factoryの可読性が低い - 型の恩恵が得ずらい
- 配列の値の一括上書きが出来ない factory-go
DRYで楽にかつ型安全で保守性が高い テスト用のデータ(struct)の初期化がしたい! - 冗長な書き方になってしまう - YAMLやinterfaceなど型の恩恵が受けられない 既存ライブラリの課題
- DRYで冗長な記述を書かない - 型安全で、補完が効く - 直感的なインターフェース fixtory
設計思想 - DRY テストケース固有の値以外は使い回す
設計思想 - 型安全 型があることで... - テスト用のデータにすぐ飛べる -> 可読性が高い - コンパイルエラーで検知できる
-> 変更に強い - コード補完が効く -> 効率◦
fixtory fixtory factory-go
0. 対象struct
1. go generate
2. struct初期化
フィールド定義の使い回し
DBへのINSERT
Blueprintのstructに対して、ひたすら上書き 内部実装
Pros - 値の設定・上書きが柔軟 - 型の恩恵が得られる Cons - go generate or
fixtoryコマンドの実行が必要 (genericsが入れば不要になる予定) - ゼロ値での上書きが面倒 - 上書きし過ぎると、可読性が下がる fixtroy
ゼロ値での上書き
まとめ ライブラリ 可読性 柔軟性 効率性 testfixtures ◎ △ △ factory-go
△ ◎ △ fixtory ◯ ◯ ◎ ユースケースに応じたライブラリ選択
まとめ 辛くて大変なテストを 出来るだけ楽で楽しいものしていきましょう!
We’re hiring! Goエンジニア募集中!