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
540
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
83
Goで作る全文検索エンジンライブラリ
kyomo
0
780
Terraform Provider開発のノウハウ
kyomo
1
170
Paper reading - ROSE: Robust Caches for Amazon Product Search
kyomo
0
36
Elasticsearch Aggregations
kyomo
0
40
AlloyDB overview
kyomo
0
77
Unsupervised synonym Extraction
kyomo
0
140
pubsub_cli - CLI Tool for Cloud Pub/Sub
kyomo
0
85
DatastoreからSpannerへのゼロダウンタイム移行
kyomo
0
100
Other Decks in Programming
See All in Programming
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
3
240
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
230
VSCodeでのDatabricks開発もお勧めしたい/I would also recommend Databricks development with VSCode.
kazumain
0
260
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
210
使ってみよう Azure AI Document Intelligence
kosmosebi
2
330
Blue/Greenデプロイの導入による 運用フローの改善
kudoas
1
390
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
450
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
190
Ruby GitHub Packages
bkuhlmann
0
630
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
500
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
2
1.3k
BBQ
matthewcrist
80
8.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Writing Fast Ruby
sferik
621
60k
Fireside Chat
paigeccino
21
2.6k
RailsConf 2023
tenderlove
4
540
WebSockets: Embracing the real-time Web
robhawkes
59
7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
122
39k
GitHub's CSS Performance
jonrohan
1025
450k
Large-scale JavaScript Application Architecture
addyosmani
504
110k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
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エンジニア募集中!