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
620
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
93
Goで作る全文検索エンジンライブラリ
kyomo
0
1k
Terraform Provider開発のノウハウ
kyomo
1
210
Paper reading - ROSE: Robust Caches for Amazon Product Search
kyomo
0
40
Elasticsearch Aggregations
kyomo
0
67
AlloyDB overview
kyomo
0
89
Unsupervised synonym Extraction
kyomo
0
240
pubsub_cli - CLI Tool for Cloud Pub/Sub
kyomo
0
100
DatastoreからSpannerへのゼロダウンタイム移行
kyomo
0
110
Other Decks in Programming
See All in Programming
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
790
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
560
Amazon S3 TablesとAmazon S3 Metadataを触ってみた / 20250201-jawsug-tochigi-s3tables-s3metadata
kasacchiful
0
170
dbt Pythonモデルで実現するSnowflake活用術
trsnium
0
160
DROBEの生成AI活用事例 with AWS
ippey
0
130
color-scheme: light dark; を完全に理解する
uhyo
3
360
GAEログのコスト削減
mot_techtalk
0
120
Amazon Q Developer Proで効率化するAPI開発入門
seike460
PRO
0
110
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
190
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
0
160
Amazon Bedrock Multi Agentsを試してきた
tm2
1
290
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
720
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
328
38k
A Philosophy of Restraint
colly
203
16k
KATA
mclloyd
29
14k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Side Projects
sachag
452
42k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
A Modern Web Designer's Workflow
chriscoyier
693
190k
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エンジニア募集中!