Shane A. Stillwell

Go Watch

Sometimes, you just want to run your unit tests on file save. In your Makefile, insert a target for unitw using go run to run a program directly (like npx does for npm).

unitw: ## Watch Unit Tests
	go run github.com/nf/watch@latest go test ./...

Now you can use make to watch your tests on file save

make unitw

NOTE:

go run needs to have the version (branch, tag, commit hash) in order to actually go out and fetch the item. This doesn’t work go run github.com/nf/watch go test ./..., since there is no version specified.

UPDATE:

This didn’t work well with unit tests because it kept running the same code (e.g. I broke the tests, but the run all passed :(

A better solution

watchexec

Rust based, install with Homebrew

Usage

watchexec -c -e go go test -v ./...

  • -c: clear screen before every run
  • -e: extensions to watch
  • go test -v ./...: this is just the command you want to run.

Works great