Testing
# Coverage for local packages except osme of them...
export PKGS=$(go list ./... | grep -vE "(gotests/gotests|.*data|templates)" | tr -s '\n' ',' | sed 's/.\{1\}$//')
go test -v -covermode=count -coverpkg=$PKGS -coverprofile=coverage.cov
# Find what tests were skipped
go test -v . | grep SKIP
# run tests 10 times + verbose output (also cleans cache)
go test -v -test.count 10 .
# clean cache
go clean -testcache
# run on 2 cores
go test -v -test.count 10 -test.cpu 2 .
# run tests (filter by name)
go test -v -run S .
# run 4 runners tests
go test -v -parallel 4 .
# run 4 runners tests
go test -race .
# json
go test -v --json .
# just compiling test code
go test --exec=/bin/true ./...
go test -c pkg
# Using jq to filter output of json based export.
go test -json | jq -s 'map(select(.Test != null)) | sort_by(.Elapsed)'
# benchmarks (+ memory)
go test -json -benchmem -run=^$ -bench .
#Test Examples
// - testing/add.go -
package matematica
// Add ...
func Add(a, b int) int { return a + b }
// - testing/add_test.go -
package matematica
import (
"fmt"
"testing"
)
var TestCasesAdd = []struct {
A, B int
Result int
}{
{1, 2, 3},
{2, 2, 4},
{5, 2, 7},
}
func TestAddTable(t *testing.T) {
for _, n := range TestCasesAdd {
t.Run(fmt.Sprintf("%d+%d=%d", n.A, n.B, n.Result), func(t *testing.T) {
t.Parallel()
if n.Result != Add(n.A, n.B) {
t.Fail()
}
})
}
}
func Benchmark_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, n := range TestCasesAdd {
Add(n.A, n.B)
}
}
}
// - testing/subtract.go -
package matematica
// Subtract
func Subtract(a, b int) int { return a - b }
// - testing/subtract_test.go -
package matematica
import (
"fmt"
"testing"
)
func TestSubstract(t *testing.T) {
if Subtract(4, 1) != 3 {
t.Error("Failed to subtract 1 from 4")
}
}
var TestsSubtraction = []struct {
A, B int
Result int
}{
{1, 2, -1},
{2, 2, 0},
{5, 2, 3},
}
func TestSubtractTable(t *testing.T) {
for _, n := range TestsSubtraction {
t.Run(fmt.Sprintf("%d-%d=%d", n.A, n.B, n.Result), func(t *testing.T) {
if n.Result != Subtract(n.A, n.B) {
t.Error("Failed")
}
})
}
}
#Tooling
#Tests Generation cweill/gotests
Tests generation with https://github.com/cweill/gotests, is by defacto standard.
// .vscode/settings.json
{
"go.generateTestsFlags": ["-named", "-parallel", "10"]
}
#Go tests wrappers
Go tests wrappers https://github.com/mfridman/tparse, https://github.com/gotestyourself/gotestsum, https://github.com/timtatt/sift
#tparse
# taskfile.yaml
tasks:
deps:install:tparse:
desc: "Install tparse binary."
cmds:
- cmd: |
URL="https://github.com/mfridman/tparse/releases/download/v0.18.0/tparse_linux_x86_64" && \
sudo curl -sSL -o /usr/local/bin/tparse "${URL}" && sudo chmod +x /usr/local/bin/tparse
vars: { VERSION: "v0.18.0" }
platforms: [linux]
- cmd: |
if [ $(which tparse) == "" ]; then
go install github.com/mfridman/tparse@latest
fi
platforms: [darwin]
go test --cover -v -coverpkg=github.com/user/package/... -coverprofile=coverage.out github.com/user/package/... -json | tparse
#gotestsum
https://github.com/gotestyourself/gotestsum
gotestsum
# With Coverage
gotestsum -- -coverprofile=cover.out ./...
gotestsum \
--jsonfile tmp.json.log \
--post-run-command "bash -c '
echo; echo Slowest tests;
gotestsum tool slowest --num 10 --jsonfile tmp.json.log'"