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
270
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
75
Communicating with Tests
jakubjarosz
0
180
Testing Go Code
jakubjarosz
0
250
Releasing Go Apps with GoReleaser
jakubjarosz
0
380
Building Environments with Ansible
jakubjarosz
0
420
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
400
Other Decks in Programming
See All in Programming
Press start. Python's next generation.
willingc
PRO
3
290
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
2
860
「人を評価する AI」の設計と実装
ryoyanara
0
280
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
280
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
850
Gradle-Plugin-in-2026.pdf
ryunen344
1
130
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
490
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
120
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
120
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.3k
Flow は今どうなっているか
mizdra
PRO
0
800
Featured
See All Featured
The Limits of Empathy - UXLibs8
cassininazir
1
620
Product Roadmaps are Hard
iamctodd
55
12k
Discover your Explorer Soul
emna__ayadi
2
1.3k
How to build a perfect <img>
jonoalderson
1
5.9k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.9k
Deep Space Network (abreviated)
tonyrice
0
280
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
550
Leo the Paperboy
mayatellez
8
2.2k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
630
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
310
The World Runs on Bad Software
bkeepers
PRO
72
12k
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