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
200
Testing Go CLI Apps with TestScript
Jakub Jarosz
February 23, 2023
Tweet
Share
More Decks by Jakub Jarosz
See All by Jakub Jarosz
Communicating with Tests
jakubjarosz
0
75
Testing Go Code
jakubjarosz
0
210
Releasing Go Apps with GoReleaser
jakubjarosz
0
340
Building Environments with Ansible
jakubjarosz
0
380
a quick look at webtest & pytest
jakubjarosz
1
1k
Data Driven Integration Testing
jakubjarosz
2
300
Multi Mechanize
jakubjarosz
0
140
Embracing Python for Software Testing - BDD Style
jakubjarosz
0
340
Testing Erlang Cluster with Python
jakubjarosz
1
120
Other Decks in Programming
See All in Programming
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
400
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
640
良いユニットテストを書こう
mototakatsu
11
3.5k
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
270
HTML/CSS超絶浅い説明
yuki0329
0
180
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
270
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
140
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
350
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
870
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
6
1.3k
バグを見つけた?それAppleに直してもらおう!
uetyo
0
220
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
370
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
340
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
200
Building an army of robots
kneath
302
44k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
GraphQLとの向き合い方2022年版
quramy
44
13k
Gamification - CAS2011
davidbonilla
80
5.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
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