Built-in testing capabilities of Go's standard library. Techniques for measuring test coverage and keeping it satisfactory and go through some mock-up techniques.
8. Unit testing
lion_test.go
package lion
import "testing"
func TestRoar(t *testing.T) {
res := Roar()
if res != "Lion roars" {
t.Error("Error")
}
}
9. Unit testing
go test -v -cover
sh-3.2# go test -v -cover
=== RUN TestRoar
--- PASS: TestRoar (0.00s)
PASS
coverage: 50.0% of statements
ok _/Users/jivko/test 0.008s
10. Coverage
$ go test -coverprofile=coverage.out
$ go tool cover -html=coverage.out
11. Test tables
弌亠 (slice) 仂 于仂亟 亳 亳亰仂亟亳 仂亶仆仂亳
type []struct{}
仄亠 仆舒 仂于亠仆亳 亠
Error / Fatal
Demo
12. Subtests
t.Run()
仗亳舒仆亠 仆舒 亠仂于亠 于 仗仂亟亠仂于亠
亰仄仂亢仆仂 亰舒 舒仍亳舒仆亠 仆舒 仗仂亟亠 亳 仗仂亟仍亢舒于舒仆亠 仆舒
仂仆仂于仆亳亠 亠仂于亠
go test -v -run MainTest/subTest
亰于亳从于舒仆亠 仆舒 仂仗亠亟亠仍亠仆舒 亞仗舒 仂 仗仂亟亠仂于亠:
go test -run regexp
13. Bash & Makefile
make test & make
丕亟仂弍仆仂 仂弍亠亟亳仆磦舒仆亠 仆舒 go 从仂仄舒仆亟亳
亠仆舒 亳仆亠亞舒亳 Jenkins
仂亟亞仂仂于从舒 仆舒 亠亟舒舒 仗亠亟亳 亳亰仗仍仆亠仆亳亠 仆舒 亠舒
14. DB mock
go-sqlmock, sqlmock
Empty interface{}
PostgreSQL, SQLite
Drivers: go test -tags=test
pg.go - // +build !test
sqlite.go - // +build test