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
34
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
450
Other Decks in Programming
See All in Programming
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
880
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
130
オーケストレーション記述言語 Katari
yukikurage
0
160
AmebaブログはKMPから卒業します
kmars0605
0
210
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
140
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
820
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
290
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
ジョジョの世界をXRで再現する
mofurune
0
180
Railsバージョンを4倍にした話
optim
0
520
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
290
R8 の設定見直しでアプリサイズ削減
andpad
0
220
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
460
Everyday Curiosity
cassininazir
0
360
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
エンジニアに許された特別な時間の終わり
watany
109
250k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Writing Fast Ruby
sferik
630
63k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
490
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
550
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
440
HDC tutorial
michielstock
2
970
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
おわり またどこかで お会いしましょう!