Created
February 5, 2022 15:28
-
-
Save EricCrosson/78cf061782e5f2d04f2539c3a7edd6b0 to your computer and use it in GitHub Desktop.
Shell Aliases for Testing in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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