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
Testing Go CLI Apps with TestScript
Search
Jakub Jarosz
February 23, 2023
Programming
260
0
Share
Testing Go CLI Apps with TestScript
Jakub Jarosz
February 23, 2023
More Decks by Jakub Jarosz
See All by Jakub Jarosz
Benchmarking - First Steps
jakubjarosz
0
68
Communicating with Tests
jakubjarosz
0
160
Testing Go Code
jakubjarosz
0
250
Releasing Go Apps with GoReleaser
jakubjarosz
0
380
Building Environments with Ansible
jakubjarosz
0
410
a quick look at webtest & pytest
jakubjarosz
1
1.2k
Data Driven Integration Testing
jakubjarosz
2
320
Multi Mechanize
jakubjarosz
0
150
Embracing Python for Software Testing - BDD Style
jakubjarosz
0
390
Other Decks in Programming
See All in Programming
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
540
[KCD Czech] eBPF Meets the GPU: Future of AI Infra Observability
doniacld
0
130
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.4k
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
990
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
100
ふつうのFeature Flag実践入門
irof
7
3.4k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
7
2.4k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
430
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
1.3k
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
2.3k
Claspは野良GASの夢をみるか
takter00
0
140
Swiftのレキシカルスコープ管理
kntkymt
0
200
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
420
The Curious Case for Waylosing
cassininazir
1
370
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
580
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Between Models and Reality
mayunak
4
320
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Typedesign – Prime Four
hannesfritz
42
3.1k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
320
The Cult of Friendly URLs
andyhume
79
6.9k
Transcript
Testing Go CLI Applications with Test Script Jakub Jarosz Belfast
Gophers Meetup - February 2023
cli applications http://blog.chriss-baumann.de/2010/07/26/because-life-is-too-short-for-manual-testing
cli applications & testscript http://blog.chriss-baumann.de/2010/07/26/because-life-is-too-short-for-manual-testing
testscript - run commands exec echo 'Hello Gophers!' stdout 'Hello
Gophers\n'
testscript - run commands ! exec cat not_existing_file.txt stderr 'cat:
not_existing_file.txt: No such file or directory'
testscript - run binaries exec hello stdout 'Hello Belfast Gophers\n'
testscript - run binaries with args exec hello Shawn stdout
'Hello Gopher, Shawn' ! stderr .
testscript - run binaries with args ! exec hello !
stdout . stderr 'usage: Hello Gopher NAME'
testscript - code coverage
testscript - cmp & golden files exec hello Kate cmp
stdout golden.txt -- golden.txt -- Hello Gopher, Kate!
testscript - exists, grep & regex exec hello Michael -o
greet.txt exists greet.out grep 'Michael' greet.out
testscript - txtar files & dir structure -- misc/file1.txt --
... -- misc/subfolder/file2.txt -- ... -- etc/file3.txt -- ...
testscript - file operations cp rm mv mkdir …
testscript - comments & test phases # Test 1 -
run hello without argos ... # Test 2 - run hello with args ... # Test 3 - run hello with invalid args ...
testscript - env vars env API_KEY= ! exec myprog stderr
'API_KEY must be set'
testscript - conditions [go1.16] exec echo 'We have at least
Go 1.16' [!arm64] exec echo 'This is a non-arm64 machine' [darwin] exec echo 'We''re on macOS'
testscript - extensions (custom functions)
testscript - background test steps
testscript - example project structure ── hello ├── go.mod ├──
go.sum ├── hello.go ├── hello_test.go └── testdata └── script └── hello.txtar
testscript.Run func TestScript(t *testing.T) { testscript.Run(t, testscript.Params{ Dir: "testdata/script", })
}
testscript.RunMain func TestMain(m *testing.M) { os.Exit(testscript.RunMain(m, map[string]func() int{ "hello": hello.RunCLI,
})) }
testscript - standalone testscript runner testscript testdata/script/*
summary • Excellent for testing CLI tools and binaries •
Runs along other tests • Saves time • Builds confidence • It’s derived directly from the code used to test Go tool itself!
resources • pkg.go.dev/github.com/rogpeppe/go-i nternal/testscript • github.com/mvdan/go-internal • bitfieldconsulting.com/books/tests • github.com/MariaLetta/free-gophers-
pack • github.com/qba73/belfast-go-meetup • github.com/qba73/habit
Thank You! Jakub Jarosz github, twitter @qba73 | linkedin.com/in/jakubjarosz