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
Tests in Go
Search
Yunosuke Yamada
October 16, 2022
Programming
160
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Tests in Go
Yunosuke Yamada
October 16, 2022
More Decks by Yunosuke Yamada
See All by Yunosuke Yamada
AI時代に成長するエンジニアに必要なスキルとは.pdf
yunosukey
0
240
Gemini CLIでもセキュアで堅牢な開発をしたい!
yunosukey
1
650
DevOps/MLOpsに学ぶエージェントの可観測性
yunosukey
1
1.2k
Agent Development Kitで作るマルチエージェントアプリケーション(AIAgent勉強会)
yunosukey
4
1.9k
Agent Development Kitで作るマルチエージェントアプリケーション(GCNT2025)
yunosukey
0
86
AIエージェントのオブザーバビリティについて
yunosukey
1
930
OpenTelemetry + LLM = OpenLLMetry!?
yunosukey
2
1.2k
クラウド開発環境Cloud Workstationsの紹介
yunosukey
0
470
フロントエンドオブザーバビリティ on Google Cloud
yunosukey
1
380
Other Decks in Programming
See All in Programming
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
130
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
280
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
390
源内ハンズオン概要編
hideg
0
210
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
340
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
500
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
170
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
300
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
2
440
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
350
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
230
Featured
See All Featured
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
490
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Embracing the Ebb and Flow
colly
88
5.1k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
950
Optimizing for Happiness
mojombo
378
71k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
What's in a price? How to price your products and services
michaelherold
247
13k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
670
Marketing to machines
jonoalderson
1
5.7k
Transcript
テストについて Golang編 2022/03/24 山田悠之介
テスト テストは大事。 自動テストで担保できる部分に関しては 自動テストをしなくてはいけない。 業務ではフロントエンドのテストについて勉強していたが、 バックエンドのテストが気になったので調べてみた。 2
目次 1. 普通のテスト 2. API のテスト 3. DB のテスト 3
interface と struct Go では interface を struct で実装することで オブジェクト指向のコードが書ける。
4
type Repository interface { FindAllTodos() ([]Todo, error) } type repository
struct { db *sql.DB } func (r repository) FindAllTodos() ([]Todo, error) { ... } // 返り値が Repository にできている func NewRepository(db *sql.DB) Repository { return repository{db} } 5
interface と struct ただしクラスベースのオブジェクト指向ではない。 struct は継承ができず、委譲を強制する言語設計になっている。 6
DB アクセスを Repository へ移譲する type UseCase interface { GetTodos() ([]Todo,
error) } type useCase struct { repoitory Repository } func NewTodoUseCase(repoitory Repository) UseCase { return useCase{ repoitory, } } func (u useCase) GetTodos() ([]Todo, error) { return u.repoitory.FindAllTodos() } 7
interface のモック Go には interface のモックを生成する仕組みが公式である (gomock)。 DI などと合わせて使えばテストでは委譲先をモックし、 今テストしたい
struct だけをテストすることができる。 mockgen -source=repository.go -destination=mock/mock_repository.go 8
mock を使ったテスト func TestGetTodos(t *testing.T) { mockRet := []model.Todo{{ID: 1,
Content: "Todo1"}} // mock 生成 ctrl := gomock.NewController(t) defer ctrl.Finish() mock := mock_repository.NewMockRepository(ctrl) // 期待する振る舞いを設定 mock.EXPECT().FindAllTodos().Return(mockRet, nil) // 注入 usecase := NewUseCase(mock) actual, _ := usecase.GetTodos() assert.Equal(t, mockRet, actual) } 9
API のテスト 10
DB のテスト Go に限った話ではないがいくつか方法がある mock を使う方法 比較的簡単だが、DB を使ったときに本当に動くかは分からない。 ORM を使う場合は生成される
SQL を再現しないといけないかも。 軽量な DB を使う(割愛) 実際の DB を使う方法 mock の逆で、初期化と後処理の方法を考える必要がある。 11
go-txdb 後処理はテスト中の DB 操作をトランザクションにして、 テストケースが完了したらロールバックすれば良い。 go-txdb というライブラリを使うとコネクションを Close するだけで Open
してからの操作をロールバック してくれる。 12
func TestCreateTodo(t *testing.T) { txdb.Register("find_all_todos", "mysql", "dsn") db, _ :=
sql.Open("find_all_todos", "dsn") defer db.Close() // 最後に閉じてロールバック repo := NewRepository(db) actual, _ := repo.CreateTodo("todo4") assert.Equal(t, &model.Todo{ID: 4, Content: "todo4"}, actual) todos, _ := repo.FindAllTodos() assert.Equal(t, 4, len(todos)) } 13