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
外部コマンドの実行を含む関数のテスト
Search
pongzu
April 23, 2022
Technology
3.2k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
外部コマンドの実行を含む関数のテスト
pongzu
April 23, 2022
More Decks by pongzu
See All by pongzu
Goで実装するブランドネットワークとの接続ポイント
pongzu
2
3.5k
カンムとGo
pongzu
2
1.8k
SQLFormatter written in Go
pongzu
2
3.1k
Other Decks in Technology
See All in Technology
エラーバジェットのアラートのタイミングを考える.pdf
kairim0
0
120
フロンティアAIのゲート化と地政学リスク
nagatsu
0
120
AAIFに入ってみた ~内から見えるコミュニティ動向~
sato4
0
160
2026.06.13_AI時代に事業会社が「SIer出身エンジニア」を求める理由 / Why Businesses Seek Engineers with a System Integrator Background in the AI Era
jumtech
0
1k
RAG を使わないという選択肢
tatsutaka
1
190
SONiC Scale-Up Working Group から探る Scale-UpやUltraEthernet機能の実装方法
ebiken
PRO
1
120
小さくはじめるSLI/SLO ~育てながら組織に定着させる実践知~ / Starting Small with SLI/SLOs: Building Adoption Through Continuous Growth
nari_ex
5
1.7k
機械学習を「社会実装」するということ 2026年夏版 / Social Implementation of Machine Learning June 2026 Version
moepy_stats
4
1.5k
NAB Show 2026 動画技術関連レポート / NAB Show 2026 Report
cyberagentdevelopers
PRO
0
170
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
130
Amazon Bedrock AgentCore ワークショップ JAWS UG TOHOKU / amazon-bedrock-agentcore-workshop-jawsug-tohoku-2026
gawa
9
690
爆速でマルチプロダクトを立ち上げる時 事業・CTO目線で大事にしたい事
miyatakoji
0
100
Featured
See All Featured
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
Writing Fast Ruby
sferik
630
63k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Unsuck your backbone
ammeep
672
58k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
The Spectacular Lies of Maps
axbom
PRO
1
800
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
Six Lessons from altMBA
skipperchong
29
4.3k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
970
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
Transcript
外部コマンドの実行を含む関数のテスト Yu Tanaka Kanmu, Inc. Go Conference 2022 Spring April
23,2022
Copyright Kanmu, Inc. All right reserved. 2 田中 悠(ぽんず) @pongzu
@_pongzu 自己紹介
Copyright Kanmu, Inc. All right reserved. バンドルカードとpoolを作ってます
Copyright Kanmu, Inc. All right reserved. 外部コマンドをコード内から実行する 1
Copyright Kanmu, Inc. All right reserved. interfaceを用いたモック 1 exec.Command()呼び出しから cmd.Output()の実行まで、まるご
とモックすればよい?
Copyright Kanmu, Inc. All right reserved. interfaceを用いたモック 6 1 •
Executor interface を定義 • Executorを受け取るrunLs()を定義 • テスト時はモックのExecutorに差し替える 書くこと多すぎるし、複雑 すぎるな...
Copyright Kanmu, Inc. All right reserved. interfaceを用いたモック 1 exec.Command()呼び出しから cmd.Output()の実行まで、まるご
とモックすればよい?
Copyright Kanmu, Inc. All right reserved. exec_test.goの実装 1 exec.Command()にビルドした テストバイナリ自身を渡して実
行バイナリ自体を差し替える
Copyright Kanmu, Inc. All right reserved. exec_test.goの実装 9 1 https://pkg.go.dev/os/execを参照
Copyright Kanmu, Inc. All right reserved. exec_test.goの実装 10 1 •
os.Args[0]は実行コマンドのパスが入る ◦ go test実行時はビルドしたテストバイナリ自体 • exec.Commandにos.Args[0]を渡すと外部コマンド としてテストバイナリ自身を実行できる https://pkg.go.dev/os/execを参照
Copyright Kanmu, Inc. All right reserved. exec_test.goの実装 11 1 •
テスト実行時に-test.run=オプションを与えて実行 処理を絞ることができる • cmd.Output()で実行されるのはテストバイナリ自体 で、その中で呼ばれるのはTestHelperProcess() https://pkg.go.dev/os/execを参照
Copyright Kanmu, Inc. All right reserved. exec_test.goの実装 12 1 https://pkg.go.dev/os/execを参照
• モックしたいコマンド名とその引数を取得 • コマンド別に任意の処理を書く • helperCommand()でセットしたテストバイ ナリ自身の呼び出し時にここに書いた処理 が実行される
最後に
Copyright Kanmu, Inc. All right reserved. 参考資料/使ったコード等 • 今回使ったコード ◦
https://github.com/pongzu/testing-exec-sample • 参考資料/コード ◦ https://npf.io/2015/06/testing-exec-command ◦ https://pkg.go.dev/os/exec
Copyright Kanmu, Inc. All right reserved. We are hiring! まだまだやりたいことはたくさん!
仲間になりませんか!
Thank you!