????3??T??
????*testing.T????????????棺
????t.Errorf("got bar = %v?? want %v"?? got?? want)
????t.Fatalf("Frobnicate(%v) returned error: %v"?? arg?? err)
????t.Logf("iteration %v"?? i)
?????????????enable???в???(parallet test)??
????t.Parallel()
?????????????????????У?
????if runtime.GOARCH == "arm" {
????t.Skip("this doesn't work on ARM")
????}
????4?????в???
??????????go test?????????????????????
?????????е??·???°?????????
????$ go test
????PASS
????$ go test -v
????=== RUN TestIndex
????— PASS: TestIndex (0.00 seconds)
????PASS
????????й????μ????в???????????????????
????$ go test github.com/nf/…
??????????????
????$ go test std
???????????strings_test.go???????testgo????testgo???????go test????OK?????????????л???testgo????????????go test?????????????????
????$go test testgo
????can't load package: package testgo: cannot find package "testgo" in any of:
????/usr/local/go/src/pkg/testgo (from $GOROOT)
????/Users/tony/Test/GoToolsProjects/src/testgo (from $GOPATH)
????????????testgo???????go test??????????????????????go test????GOROOT??GOPATH?2????????????а???????
????5???????????
????go tool??????????????????????
??????????testgo?????go test -cover????????£?
????go build _/Users/tony/Test/Go/testgo: no buildable Go source files in /Users/tony/Test/Go/testgo
????FAIL    _/Users/tony/Test/Go/testgo [build failed]
??????????cover???????????????????????????????????б??????????????????????????
????????????л???$GOROOT/src/pkg/strings?£????go test -cover??
????$go test -v -cover
????=== RUN TestReader
????— PASS: TestReader (0.00 seconds)
????… …
????=== RUN: ExampleTrimPrefix
????— PASS: ExampleTrimPrefix (1.75us)
????PASS
????coverage: 96.9% of statements
????ok      strings    0.612s
????go test??????????????profile????????????????go tool cover?????????
??????$GOROOT/src/pkg/strings??????У?
????$ go test -coverprofile=cover.out
??????????????????cover.out?????
??????cover.out????????????????
????a) cover -func=cover.out
????$sudo go tool cover -func=cover.out
????strings/reader.go:24:    Len                66.7%
????strings/reader.go:31:    Read                100.0%
????strings/reader.go:44:    ReadAt                100.0%
????strings/reader.go:59:    ReadByte            100.0%
????strings/reader.go:69:    UnreadByte            100.0%
????… …
????strings/strings.go:638:    Replace                100.0%
????strings/strings.go:674:    EqualFold            100.0%
????total:            (statements)            96.9%
????b) ???????
???????go tool cover -html=cover.out????????/tmp??????????coverxxxxxxx??????/tmp/cover404256298??????????? coverage.html??????????????coverage.html???????????????????????????????
????????go tool??cover???????go version go1.3 darwin/amd64?????????????????go get?????
????$sudo GOPATH=/Users/tony/Test/GoToolsProjects go get code.google.com/p/go.tools/cmd/cover
?????????cover?????$GOROOT/pkg/tool/darwin_amd64???檔
??????????????????
????1????????????
????outyet?????web??????????????????Go?汾???????????????????????????
????go get github.com/golang/example/outyet
???????
????go get??к?cd $GOPATH/src/github.com/golang/example/outyet?£????go run main.go??????????????http://localhost:8080????????Web???????
????2??????Http??????????
????net/http/httptest???????????????????????????Щ???????Http????????
????3??httptest.Server
????httptest.Server?????????????????????????listen???????????????HTTP?????
????type Server struct {
????URL      string // base URL of form http://ipaddr:port with no trailing slash
????Listener net.Listener
????// TLS is the optional TLS configuration?? populated with a new config
????// after TLS is started. If set on an unstarted server before StartTLS
????// is called?? existing fields are copied into the new config.
????TLS *tls.Config
????// Config may be changed after calling NewUnstartedServer and
????// before Start or StartTLS.
????Config *http.Server
????}
????func NewServer(handler http.Handler) *Server
????func (*Server) Close() error
????4??httptest.Server??
?????????????????????Http Server?????????Hello???
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter?? r *http.Request) {
fmt.Fprintln(w?? "Hello?? client")
}))
defer ts.Close()
res?? err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting?? err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s"?? greeting)
????5??httptest.ResponseRecorder
????httptest.ResponseRecorder??http.ResponseWriter?????????????????仯??????????????????С?
????type ResponseRecorder struct {
????Code      int           // the HTTP response code from WriteHeader
????HeaderMap http.Header   // the HTTP response headers
????Body      *bytes.Buffer // if non-nil?? the bytes.Buffer to append written data to
????Flushed   bool
????}
????6??httptest.ResponseRecorder??
?????????HTTP handler?д??????ResponseRecorder????????????????????????????
????handler := func(w http.ResponseWriter?? r *http.Request) {
????http.Error(w?? "something failed"?? http.StatusInternalServerError)
????}
????req?? err := http.NewRequest("GET"?? "http://example.com/foo"?? nil)
????if err != nil {
????log.Fatal(err)
????}
????w := httptest.NewRecorder()
????handler(w?? req)
????fmt.Printf("%d – %s"?? w.Code?? w.Body.String())