Quick go test cycle with reflex
While you are working on some piece of code, it is nice to have some feedback about whether you broke or fixed something by running the relevant unit tests. To automate this I usually have a terminal window open with the following command:
$ reflex -r '\.go$' -- \
sh -c 'clear && go test -v ./web/model --run TestEvent'
Breakdown:
reflexis a small utilty that watches for changes on the file system.-rindiates that it should only watch changes in files that satisfy the following regex pattern.'\.go$'the regex tells to only watch changes in go files.--signifies the end of reflex options.shshell to interpret the command we want to run.-ctell sh to listen to commands entered after, not to standard input.clearfirst clear the terminal window.go testrun the test in verbose mode.-vwill produce a more verbose output with PASS/FAIL for each test and the output fom t.Log,./web/modelonly test files in the web/model package.-runTestEvent only run test with names that satisfy the regext TestEvent.
Remember, reflex is only triggered by changes on the filesystem. After entering the command, nothing happens until you save a .go file.
Sources
- Reflex - github.com
- What does '--' (double dash / double hyphen) mean? - unix.stackexchange.com
- man sh
- Golang basics - writing unit tests - blog.alexellis.io
- go command - Testing flags - pkg.go.dev