Skip to content

Instantly share code, notes, and snippets.

@EricCrosson
Created February 5, 2022 15:28
Show Gist options
  • Save EricCrosson/78cf061782e5f2d04f2539c3a7edd6b0 to your computer and use it in GitHub Desktop.
Save EricCrosson/78cf061782e5f2d04f2539c3a7edd6b0 to your computer and use it in GitHub Desktop.
Shell Aliases for Testing in Go
# Calculate test-coverage over a set of tests identified by a regex
# Usage: gocover <package> [<regex>]
#
# @example
# gocover ./service/foo 'TestFoo/Behavior/.*'
gocover() {
local package="${1:?Package must be specified}"
local regex="${2:+-run $2}"
local t="$(mktemp -t cover-XXXXXXXXXX)"
go test "${package}" -v -coverprofile="${t}" ${regex}
go tool cover -func="${t}"
unlink "${t}"
}
# Visualize test-coverage in a browser over a set of tests identified by a regex
# Usage: gocover <package> [<regex>]
#
# @example
# gocoverage ./service/foo 'TestFoo/Behavior/.*'
gocoverage() {
local package="${1:?Package must be specified}"
local regex="${2:+-run $2}"
local t="$(mktemp -t cover-XXXXXXXXXX)"
go test "${package}" -v -coverprofile="${t}" ${regex}
go tool cover -html="${t}"
rm -r "${t}"
}
# Run a set of tests identified by a regex
# Usage: gotest <package> [<regex>]
#
# @example
# gotest ./service/foo 'TestFoo/Behavior/.*'
gotest() {
local package="${1:?Package must be specified}"
local regex="${2:+-run $2}"
go test "${package}" -v -cover ${regex}
}
# Watch a set of tests identified by a regex with nodemon
# Usage: gotestwatch <package> [<regex>]
#
# @example
# gotestwatch ./service/foo 'TestFoo/Behavior/.*'
gotestwatch() {
local package="${1:?Package must be specified}"
local regex="${2:+-run $2}"
nodemon -x "go test ${package} -v -cover ${regex} || true"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment