Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
外部コマンドの実行を含む関数のテスト
pongzu
April 23, 2022
Technology
0
310
外部コマンドの実行を含む関数のテスト
pongzu
April 23, 2022
Tweet
Share
More Decks by pongzu
See All by pongzu
カンムとGo
pongzu
2
550
SQLFormatter written in Go
pongzu
2
1.8k
Other Decks in Technology
See All in Technology
AWS Control TowerとAWS Organizationsを活用した組織におけるセキュリティ設定
fu3ak1
2
620
HTTP Session Architecture Pattern
chiroito
1
390
様々な現場のPower Platform ~小さなエンジニアの奮闘記~
hyodol2513
0
1.9k
Microsoft 365の中でのPower BIの利用 / M365VM2022
ishiayaya
0
1.5k
runn is a package/tool for running operations following a scenario. / golang.tokyo #32
k1low
1
120
暗号資産ウォレット入門(MetaMaskの入門~NFTの購入~詐欺の注意事項など)
kayato
2
180
Microsoft Power Automate で 始めるRPAと自動化
taikiyoshida
0
2k
Research Paper Introduction #98 "NSDI 2022 recap"
cafenero_777
0
200
スタートアップ入社4日目までに考えたAWSのセキュリティ向上/ Startup AWS Security
shonansurvivors
3
2.8k
AWSの基礎を学ぼうで学んだ9種類のDBを勝手にふりかえる
98lerr
2
720
Power BI ”を” 可視化しよう!
hanaseleb
0
150
Babylon.js v5 新機能の紹介
limes2018
1
1.1k
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
63
7.5k
Intergalactic Javascript Robots from Outer Space
tanoku
261
25k
Imperfection Machines: The Place of Print at Facebook
scottboms
253
11k
A Tale of Four Properties
chriscoyier
149
20k
StorybookのUI Testing Handbookを読んだ
zakiyama
4
2k
Principles of Awesome APIs and How to Build Them.
keavy
113
15k
Code Reviewing Like a Champion
maltzj
506
37k
Side Projects
sachag
449
37k
No one is an island. Learnings from fostering a developers community.
thoeni
9
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
3
430
Happy Clients
brianwarren
89
5.5k
Bootstrapping a Software Product
garrettdimon
295
110k
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!