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
0
220
Testing Go CLI Apps with TestScript
Jakub Jarosz
February 23, 2023
Tweet
Share
More Decks by Jakub Jarosz
See All by Jakub Jarosz
Benchmarking - First Steps
jakubjarosz
0
26
Communicating with Tests
jakubjarosz
0
110
Testing Go Code
jakubjarosz
0
220
Releasing Go Apps with GoReleaser
jakubjarosz
0
350
Building Environments with Ansible
jakubjarosz
0
390
a quick look at webtest & pytest
jakubjarosz
1
1.1k
Data Driven Integration Testing
jakubjarosz
2
310
Multi Mechanize
jakubjarosz
0
140
Embracing Python for Software Testing - BDD Style
jakubjarosz
0
370
Other Decks in Programming
See All in Programming
プロダクトという一杯を作る - プロダクトチームが味の責任を持つまでの煮込み奮闘記
hiliteeternal
0
220
DMMを支える決済基盤の技術的負債にどう立ち向かうか / Addressing Technical Debt in Payment Infrastructure
yoshiyoshifujii
4
620
顧客の画像データをテラバイト単位で配信する 画像サーバを WebP にした際に起こった課題と その対応策 ~継続的な取り組みを添えて~
takutakahashi
4
1.4k
テストから始めるAgentic Coding 〜Claude Codeと共に行うTDD〜 / Agentic Coding starts with testing
rkaga
17
6k
Android 16KBページサイズ対応をはじめからていねいに
mine2424
0
660
Startups on Rails in Past, Present and Future–Irina Nazarova, RailsConf 2025
irinanazarova
0
300
知って得する@cloudflare_vite-pluginのあれこれ
chimame
1
120
No Install CMS戦略 〜 5年先を見据えたフロントエンド開発を考える / no_install_cms
rdlabo
0
320
20250708_JAWS_opscdk
takuyay0ne
2
150
型で語るカタ
irof
1
830
「次に何を学べばいいか分からない」あなたへ──若手エンジニアのための学習地図
panda_program
3
650
Streamlitで実現できるようになったこと、実現してくれたこと
ayumu_yamaguchi
2
220
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
990
Documentation Writing (for coders)
carmenintech
72
4.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Code Review Best Practice
trishagee
69
19k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Scaling GitHub
holman
461
140k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
A Modern Web Designer's Workflow
chriscoyier
695
190k
YesSQL, Process and Tooling at Scale
rocio
173
14k
The Language of Interfaces
destraynor
158
25k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
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