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
t *testing.T は どこからやってくるの?
Search
Kotaro Otaka
April 22, 2026
Programming
1.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
t *testing.T は どこからやってくるの?
The Go gopher was designed by Renée French.
https://gotalk.connpass.com/event/388084/
Kotaro Otaka
April 22, 2026
More Decks by Kotaro Otaka
See All by Kotaro Otaka
Go で「見つからなかった」をどう表現するか
otakakot
0
33
actions/setup-go 完全に理解した
otakakot
1
130
Vendoring をやめたけどやめたのは正解だったのか
otakakot
0
70
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
1.5k
Go で作った CLI ツールにバージョンを埋め込みたい
otakakot
0
260
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
2
670
組込みだけじゃない! TinyGo で始める無料クラウド開発入門
otakakot
1
180
統一感のある Go コードを生成 AI の力で手にいれる
otakakot
1
3.9k
SQLBoiler がメンテナンスモードになりまして
otakakot
0
440
Other Decks in Programming
See All in Programming
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
280
The Rails Doctrine Decade
koic
2
600
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
280
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
330
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
3
760
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
270
気持ちぃ〜角丸 Squircle
ryu1013
0
320
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
220
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
870
sbt 2.0: takeoff
eed3si9n
0
160
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
410
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
870
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
310
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Discover your Explorer Soul
emna__ayadi
2
1.3k
KATA
mclloyd
PRO
35
16k
Transcript
t *testing.T は どこからやってくるの? おーたかこーたろー / @otakakot 2026.04.22 Go Connect
#12 The Go gopher was designed by Renée French.
ある日... #goconnect
データベースを使ったテストを 実装していたときの話 #goconnect
#goconnect
context canceled が発生して クリーンアップ処理に失敗 #goconnect
pgx パッケージを確認 #goconnect
t.Context() がキャンセルされている #goconnect
Bad! Good! #goconnect
t.Cleanup() では t.Context() は キャンセル済み! #goconnect
ところで... #goconnect
この t *testing.Tはどこからくるのか #goconnect
そもそも Go は main パッケージの main 関数が実行されるんでしょ #goconnect
#goconnect
なぜこのコードは動く ... ??? #goconnect
TextXxx は黒魔術...??? #goconnect
go test が動く仕組み #goconnect
おさらい #goconnect
#goconnect How to Write Go Code #Testing go test コマンドと
testing パッケージ _test.go で終わるファイル func TestXXX(t *testing.T) 関数 https://go.dev/doc/code#Testing
#goconnect https://go.dev/doc/code#Testing
結論 #goconnect
#goconnect go test は ... テスト関数を呼び出す Go コード (main関数)を自動生成し コンパイルして実行している!
全体像 go test コマンド実行 ファイルの分類・関数の収集 _testmain.go の生成 ビルド 実行 #goconnect
注釈 ステップ数はおーたかによる解釈 #goconnect
サンプルコード #goconnect
go version Go 1.26.2 #goconnect
パッケージ構成 #goconnect
#goconnect
#goconnect
#goconnect
注釈 Go 本体のコードを引用しますが スライドの都合で省略しています 詳しくはURLを添付しているので そちらをご参照ください #goconnect
go test コマンド実行 #goconnect
概要 go コマンドが test サブコマンドを呼び テスト処理を実行する #goconnect
軽く解説 #goconnect
#goconnect ざっくりの流れ... go コマンド → test サブコマンド https://github.com/golang/go/blob/go1.26.2/src/cmd/go/main.go
#goconnect ざっくりの流れ... func runTest(...) に到達 https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/test/test.go
rutTest() 関数は パッケージごとに build → run → print を行うオーケストレーション #goconnect
build 処理にて テスト対象ファイル・関数を収集する Import(...) 関数に到達 #goconnect
#goconnect ファイルの分類・関数の収集
概要 ファイル名とパッケージ宣言で分類 テスト名をもとにテスト関数を収集 #goconnect
ファイルとパッケージの分類 ファイル名が _test.go で終わるか パッケージ宣言が _test で終わるか #goconnect
グルーピング ファイル名 パッケージ宣言 GoFiles ex) calc.go ex) package calc TestGoFiles
ex) calc_test.go ex) package calc XTestGoFiles ex) calc_x_test.go ex) package calc_test #goconnect
グルーピング ファイル名 パッケージ宣言 GoFiles ex) calc.go ex) package calc TestGoFiles
ex) calc_test.go ex) package calc XTestGoFiles ex) calc_x_test.go ex) package calc_test pxtest ptest #goconnect
各パッケージからテスト関数を AST により収集 #goconnect
#goconnect ptest, pxtest を for ループ https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L584-L593
#goconnect テスト関数を管理する構造体 https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L626-L638
#goconnect ファイル内の関数をループ https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L702-L773
#goconnect 命名規則チェック 関数の型チェック https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L702-L773
#goconnect https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L584-L593
#goconnect 型チェック ジェネリクス禁止 https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L775-L788
#goconnect 戻り値・引数チェック ポインタチェック 型名チェック https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L557-L579
以下のテスト関数を収集 ptest.TestAdd(t *testing.T) ptest.TestSub(t *testing.T) pxtest.TestAdd(t *testing.T) #goconnect
_testmain.go の生成 #goconnect
概要 テスト関数を実行する main () 関数を生成する #goconnect
Go Template を使ったコード生成 testFuncs 構造体を受け取る testmainTmpl #goconnect
#goconnect https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L790-L862
#goconnect https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L790-L862
#goconnect https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L790-L862
#goconnect Go Template によるコード生成 https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/load/test.go#L617-L624
生成されるテスト実行コード #goconnect
_testmain.go の確認方法 go test -work WORK=xxxx #goconnect
#goconnect https://github.com/golang/go/blob/go1.26.2/src/testing/testing.go#L2319-L2329
#goconnect https://github.com/golang/go/blob/go1.26.2/src/testing/testing.go#L2340-L2487
#goconnect https://github.com/golang/go/blob/go1.26.2/src/testing/testing.go#L2551-L2598
t *testing.T は自動生成するコードで テストを動かす MainStart(..).Run() 関数 からやってくる! #goconnect
#goconnect https://github.com/golang/go/blob/go1.26.2/src/testing/testing.go#L1876-L2042
ビルド #goconnect
概要 リスト化したパッケージをもとに 実行可能バイナリを生成する #goconnect
省略! go build と同じ仕組みを利用 バイナリの命名が異なる (.test が付与) #goconnect
実行 #goconnect
概要 生成したテストバイナリを 子プロセスとして実行 #goconnect
#goconnect https://github.com/golang/go/blob/go1.26.2/src/cmd/go/internal/test/test.go#L1435-L1774
テスト実行完了! #goconnect
まとめ #goconnect
#goconnect go test は黒魔術ではなく go template と AST を愚直に使った コード自動生成による
main 関数の実行
2020.04 2021.04 2022.11 Kotaro Otaka おーたかこーたろー / @otakakot Web Engineer
新卒入社 Hello Golang! Web Engineer @株式会社ビットキー Gopher6年生 #goconnect
おわり またどこかで お会いしましょう!