Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Go CLI Apps with TestScript

Jakub Jarosz
February 23, 2023

Testing Go CLI Apps with TestScript

Jakub Jarosz

February 23, 2023
Tweet

More Decks by Jakub Jarosz

Other Decks in Programming

Transcript

  1. testscript - run commands ! exec cat not_existing_file.txt stderr 'cat:

    not_existing_file.txt: No such file or directory'
  2. testscript - run binaries with args ! exec hello !

    stdout . stderr 'usage: Hello Gopher NAME'
  3. testscript - cmp & golden files exec hello Kate cmp

    stdout golden.txt -- golden.txt -- Hello Gopher, Kate!
  4. testscript - exists, grep & regex exec hello Michael -o

    greet.txt exists greet.out grep 'Michael' greet.out
  5. testscript - txtar files & dir structure -- misc/file1.txt --

    ... -- misc/subfolder/file2.txt -- ... -- etc/file3.txt -- ...
  6. testscript - comments & test phases # Test 1 -

    run hello without argos ... # Test 2 - run hello with args ... # Test 3 - run hello with invalid args ...
  7. 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'
  8. testscript - example project structure ── hello ├── go.mod ├──

    go.sum ├── hello.go ├── hello_test.go └── testdata └── script └── hello.txtar
  9. 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!