Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
外部コマンドの実行を含む関数のテスト
Search
pongzu
April 23, 2022
Technology
0
3k
外部コマンドの実行を含む関数のテスト
pongzu
April 23, 2022
Tweet
Share
More Decks by pongzu
See All by pongzu
Goで実装するブランドネットワークとの接続ポイント
pongzu
2
3.4k
カンムとGo
pongzu
2
1.7k
SQLFormatter written in Go
pongzu
2
3k
Other Decks in Technology
See All in Technology
100以上の新規コネクタ提供を可能にしたアーキテクチャ
ooyukioo
0
220
AIBuildersDay_track_A_iidaxs
iidaxs
4
930
AWS Security Agentの紹介/introducing-aws-security-agent
tomoki10
0
380
Entity Framework Core におけるIN句クエリ最適化について
htkym
0
100
2025年 開発生産「可能」性向上報告 サイロ解消からチームが能動性を獲得するまで/ 20251216 Naoki Takahashi
shift_evolve
PRO
2
210
Database イノベーショントークを振り返る/reinvent-2025-database-innovation-talk-recap
emiki
0
250
20251222_next_js_cache__1_.pdf
sutetotanuki
0
110
AWS re:Invent 2025 re:Cap LT大会 データベース好きが語る re:Invent 2025 データベースアップデート/セッションの紹介
coldairflow
0
140
IAMユーザーゼロの運用は果たして可能なのか
yama3133
2
520
AIエージェント開発と活用を加速するワークフロー自動生成への挑戦
shibuiwilliam
4
760
[Data & AI Summit '25 Fall] AIでデータ活用を進化させる!Google Cloudで作るデータ活用の未来
kirimaru
0
150
AIプラットフォームにおけるMLflowの利用について
lycorptech_jp
PRO
1
180
Featured
See All Featured
BBQ
matthewcrist
89
9.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
エンジニアに許された特別な時間の終わり
watany
105
220k
Practical Orchestrator
shlominoach
190
11k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Color Theory Basics | Prateek | Gurzu
gurzu
0
140
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
0
290
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
31
Six Lessons from altMBA
skipperchong
29
4.1k
[RailsConf 2023] Rails as a piece of cake
palkan
58
6.2k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
33
The SEO Collaboration Effect
kristinabergwall1
0
300
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!