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
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
110
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
330
pubsub_cli - CLI Tool for Cloud Pub/Sub
kyomo
0
130
DatastoreからSpannerへのゼロダウンタイム移行
kyomo
0
120
Other Decks in Programming
See All in Programming
CSC307 Lecture 08
javiergs
PRO
0
670
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.7k
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
180
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
180
Apache Iceberg V3 and migration to V3
tomtanaka
0
170
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
260
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
210
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
200
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
280
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
230
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
140
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
37
7.1k
Test your architecture with Archunit
thirion
1
2.2k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
170
Utilizing Notion as your number one productivity tool
mfonobong
3
220
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
From π to Pie charts
rasagy
0
130
Google's AI Overviews - The New Search
badams
0
910
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
110
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
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エンジニア募集中!