1 回答
TA贡献1807条经验 获得超9个赞
用法:
go test [-c] [-i] [build and test flags] [packages] [flags for test binary]
例如,
飞跃/leap.go
package leap
func IsLeap(year int) bool {
return true
}
飞跃/leap_test.go
package leap
import (
"testing"
)
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "a vanilla leap year"},
{1997, false, "a normal year"},
{1900, false, "a century"},
{2400, true, "an exceptional century"},
}
func TestLeapYears(t *testing.T) {
for _, test := range testCases {
observed := IsLeap(test.year)
if observed != test.expected {
t.Fatalf("%v is %s", test.year, test.description)
}
}
}
如果$GOPATH设置为包含leap包目录:
$ go test leap
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
FAIL leap 0.003s
$
或者,如果你cd到leap包目录:
$ go test
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
exit status 1
FAIL so/leap 0.003s
$
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报