????????????????????Golang???????????Andrew Gerrand??Google I/O 2014????????????“Testing Techniques”???????????Golang???? ??????????????????????????????????????????????????????????????????mock/fake??????????????????????????/???? ???vet??????????о????????棬???????????????????????????????????Slide????????????????????????????2?? ?£?Golang??????slide????????????е?golang artical???????????????????http://go-talks.appspot.com/???????????????????pdf????????????????ú??????????
??????????????????
????1??????Go????
????Go???????ò??????
?????????????????testing?????go test????????????????
??????????????????????strings.Index???????????????????
//strings_test.go (???????????????strings_test.go?????)
package strings_test
import (
"strings"
"testing"
)
func TestIndex(t *testing.T) {
const s?? sep?? want = "chicken"?? "ken"?? 4
got := strings.Index(s?? sep)
if got != want {
t.Errorf("Index(%q??%q) = %v; want %v"?? s?? sep?? got?? want)//????slide?е?got??wantд????
}
}
$go test -v strings_test.go
=== RUN TestIndex
— PASS: TestIndex (0.00 seconds)
PASS
ok      command-line-arguments    0.007s
go test??-v???????????????????????
???????е?want?????????3?????????????????????????
$go test -v strings_test.go
=== RUN TestIndex
— FAIL: TestIndex (0.00 seconds)
strings_test.go:12: Index("chicken"??"ken") = 4; want 3
FAIL
exit status 1
FAIL    command-line-arguments    0.008s
????2????????????
Golang??struct?????(struct literals)???????????????д?????????????
package strings_test
import (
"strings"
"testing"
)
func TestIndex(t *testing.T) {
var tests = []struct {
s   string
sep string
out int
}{
{""?? ""?? 0}??
{""?? "a"?? -1}??
{"fo"?? "foo"?? -1}??
{"foo"?? "foo"?? 0}??
{"oofofoofooo"?? "f"?? 2}??
// etc
}
for _?? test := range tests {
actual := strings.Index(test.s?? test.sep)
if actual != test.out {
t.Errorf("Index(%q??%q) = %v; want %v"??
test.s?? test.sep?? actual?? test.out)
}
}
}
$go test -v strings_test.go
=== RUN TestIndex
— PASS: TestIndex (0.00 seconds)
PASS
ok      command-line-arguments    0.007s