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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
220
Gemini CLIでもセキュアで堅牢な開発をしたい!
yunosukey
1
640
DevOps/MLOpsに学ぶエージェントの可観測性
yunosukey
1
1.2k
Agent Development Kitで作るマルチエージェントアプリケーション(AIAgent勉強会)
yunosukey
4
1.8k
Agent Development Kitで作るマルチエージェントアプリケーション(GCNT2025)
yunosukey
0
85
AIエージェントのオブザーバビリティについて
yunosukey
1
920
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
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
530
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
450
React本体のコードリーディング
high_g_engineer
1
130
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.4k
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
200
Foundation Models frameworkで画像分析
ryodeveloper
1
510
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
200
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.8k
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
200
メールのエイリアス機能を履き違えない
isshinfunada
0
220
Android CLI
fornewid
0
210
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
330
Featured
See All Featured
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Paper Plane
katiecoart
PRO
2
52k
Abbi's Birthday
coloredviolet
3
9.1k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
600
A Tale of Four Properties
chriscoyier
163
24k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
We Are The Robots
honzajavorek
0
290
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
450
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