Writing basic tests in Go is:
- creating a file with the suffix
*_test.go
- definig a function with the signature
TestXxx(t *testing.T)
- running it using the
go test
command
go mod init add
go mod tidy
// add.go
package add
func Add(a ...int) int {
total := 0
for i := range a {
total += a[i]
}
return total
}
// add_test.go
package add
import "testing"
func TestAdd(t *testing.T) {
// case 1
if res := Add(1, 2, 3); res != 6 {
t.Errorf("Expected %d instead of %d", 6, res)
}
// case 2
if res := Add(-1, -2); res != -3 {
t.Errorf("Expected %d instead of %d", -3, res)
}
}
go test
go test -v # verbose mode
go test -v -- -test.run ^TestAdd$
go test -run ^TestAdd$ -v
go test -run "^TestAdd$" -v
Further info: