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
0
2.9k
外部コマンドの実行を含む関数のテスト
pongzu
April 23, 2022
Tweet
Share
More Decks by pongzu
See All by pongzu
Goで実装するブランドネットワークとの接続ポイント
pongzu
2
3.2k
カンムとGo
pongzu
2
1.6k
SQLFormatter written in Go
pongzu
2
2.9k
Other Decks in Technology
See All in Technology
登壇ネタの見つけ方 / How to find talk topics
pinkumohikan
3
350
Claude Code Actionを使ったコード品質改善の取り組み
potix2
PRO
6
2.1k
AWS Summit Japan 2025 Community Stage - App workflow automation by AWS Step Functions
matsuihidetoshi
1
220
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
170
GeminiとNotebookLMによる金融実務の業務革新
abenben
0
220
VCpp Link and Library - C++ breaktime 2025 Summer
harukasao
0
240
[TechNight #90-1] 本当に使える?ZDMの新機能を実践検証してみた
oracle4engineer
PRO
3
160
ローカルLLMでファインチューニング
knishioka
0
140
変化する開発、進化する体系時代に適応するソフトウェアエンジニアの知識と考え方(JaSST'25 Kansai)
mizunori
1
200
Microsoft Build 2025 技術/製品動向 for Microsoft Startup Tech Community
torumakabe
2
250
“社内”だけで完結していた私が、AWS Community Builder になるまで
nagisa53
1
340
AWS テクニカルサポートとエンドカスタマーの中間地点から見えるより良いサポートの活用方法
kazzpapa3
2
500
Featured
See All Featured
Optimizing for Happiness
mojombo
379
70k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
790
The Pragmatic Product Professional
lauravandoore
35
6.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Designing Experiences People Love
moore
142
24k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
How to Ace a Technical Interview
jacobian
277
23k
Automating Front-end Workflow
addyosmani
1370
200k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Producing Creativity
orderedlist
PRO
346
40k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
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!