Golang???????
???????????? ???????[ 2015/5/26 10:38:00 ] ??????????????????
????????????????????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
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11