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.3k
カンムとGo
pongzu
2
1.7k
SQLFormatter written in Go
pongzu
2
3k
Other Decks in Technology
See All in Technology
全員が手を動かす組織へ - 生成AIが変えるTVerの開発現場 / everyone-codes-genai-transforms-tver-development
tohae
0
190
Foundation Model × VisionKit で実現するローカル OCR
sansantech
PRO
1
370
Amazon Q と『音楽』-ゲーム音楽もAmazonQで作成してみた感想-
senseofunity129
0
140
AIのグローバルトレンド 2025 / ai global trend 2025
kyonmm
PRO
1
140
Infrastructure as Prompt実装記 〜Bedrock AgentCoreで作る自然言語インフラエージェント〜
yusukeshimizu
1
120
ユーザー課題を愛し抜く――AI時代のPdM価値
kakehashi
PRO
1
120
AIエージェントを現場で使う / 2025.08.07 著者陣に聞く!現場で活用するためのAIエージェント実践入門(Findyランチセッション)
smiyawaki0820
6
1.1k
大規模イベントに向けた ABEMA アーキテクチャの遍歴 ~ Platform Strategy 詳細解説 ~
nagapad
0
230
生成AIによるデータサイエンスの変革
taka_aki
0
3k
o11yツールを乗り換えた話
tak0x00
2
1.4k
10年以上続くプロダクトで今取り組んでること、取り組もうとしていること
sansantech
PRO
2
110
はじめての転職講座/The Guide of First Career Change
kwappa
4
3.9k
Featured
See All Featured
Embracing the Ebb and Flow
colly
86
4.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
880
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Speed Design
sergeychernyshev
32
1.1k
Optimizing for Happiness
mojombo
379
70k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Designing Experiences People Love
moore
142
24k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
It's Worth the Effort
3n
185
28k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
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!