Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Tests in Go
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yunosuke Yamada
October 16, 2022
Programming
170
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
260
Gemini CLIでもセキュアで堅牢な開発をしたい!
yunosukey
1
660
DevOps/MLOpsに学ぶエージェントの可観測性
yunosukey
1
1.2k
Agent Development Kitで作るマルチエージェントアプリケーション(AIAgent勉強会)
yunosukey
4
1.9k
Agent Development Kitで作るマルチエージェントアプリケーション(GCNT2025)
yunosukey
0
94
AIエージェントのオブザーバビリティについて
yunosukey
1
940
OpenTelemetry + LLM = OpenLLMetry!?
yunosukey
2
1.2k
クラウド開発環境Cloud Workstationsの紹介
yunosukey
0
480
フロントエンドオブザーバビリティ on Google Cloud
yunosukey
1
390
Other Decks in Programming
See All in Programming
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
200
一参加者から『中の人』へ 〜全通PHPerがブースに立って学んだ、カンファレンスを100倍楽しむコツ〜
wp_daisuke
0
140
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
290
Intent as Code
shoppingjaws
6
970
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
470
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
110
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
120
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2k
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
170
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
660
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
300
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
56
38k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
58k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
320
Why Our Code Smells
bkeepers
PRO
340
58k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
510
AI: The stuff that nobody shows you
jnunemaker
PRO
9
990
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
220
How to Talk to Developers About Accessibility
jct
2
540
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
270
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
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