Slide 1

Slide 1 text

Testing Go CLI Applications with Test Script Jakub Jarosz Belfast Gophers Meetup - February 2023

Slide 2

Slide 2 text

cli applications http://blog.chriss-baumann.de/2010/07/26/because-life-is-too-short-for-manual-testing

Slide 3

Slide 3 text

cli applications & testscript http://blog.chriss-baumann.de/2010/07/26/because-life-is-too-short-for-manual-testing

Slide 4

Slide 4 text

testscript - run commands exec echo 'Hello Gophers!' stdout 'Hello Gophers\n'

Slide 5

Slide 5 text

testscript - run commands ! exec cat not_existing_file.txt stderr 'cat: not_existing_file.txt: No such file or directory'

Slide 6

Slide 6 text

testscript - run binaries exec hello stdout 'Hello Belfast Gophers\n'

Slide 7

Slide 7 text

testscript - run binaries with args exec hello Shawn stdout 'Hello Gopher, Shawn' ! stderr .

Slide 8

Slide 8 text

testscript - run binaries with args ! exec hello ! stdout . stderr 'usage: Hello Gopher NAME'

Slide 9

Slide 9 text

testscript - code coverage

Slide 10

Slide 10 text

testscript - cmp & golden files exec hello Kate cmp stdout golden.txt -- golden.txt -- Hello Gopher, Kate!

Slide 11

Slide 11 text

testscript - exists, grep & regex exec hello Michael -o greet.txt exists greet.out grep 'Michael' greet.out

Slide 12

Slide 12 text

testscript - txtar files & dir structure -- misc/file1.txt -- ... -- misc/subfolder/file2.txt -- ... -- etc/file3.txt -- ...

Slide 13

Slide 13 text

testscript - file operations cp rm mv mkdir …

Slide 14

Slide 14 text

testscript - comments & test phases # Test 1 - run hello without argos ... # Test 2 - run hello with args ... # Test 3 - run hello with invalid args ...

Slide 15

Slide 15 text

testscript - env vars env API_KEY= ! exec myprog stderr 'API_KEY must be set'

Slide 16

Slide 16 text

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'

Slide 17

Slide 17 text

testscript - extensions (custom functions)

Slide 18

Slide 18 text

testscript - background test steps

Slide 19

Slide 19 text

testscript - example project structure ── hello ├── go.mod ├── go.sum ├── hello.go ├── hello_test.go └── testdata └── script └── hello.txtar

Slide 20

Slide 20 text

testscript.Run func TestScript(t *testing.T) { testscript.Run(t, testscript.Params{ Dir: "testdata/script", }) }

Slide 21

Slide 21 text

testscript.RunMain func TestMain(m *testing.M) { os.Exit(testscript.RunMain(m, map[string]func() int{ "hello": hello.RunCLI, })) }

Slide 22

Slide 22 text

testscript - standalone testscript runner testscript testdata/script/*

Slide 23

Slide 23 text

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!

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Thank You! Jakub Jarosz github, twitter @qba73 | linkedin.com/in/jakubjarosz