|
| 1 | +Testing in Go |
| 2 | +2 Mar 2020 |
| 3 | + |
| 4 | +badboyd |
| 5 | +https://github.com/badboyd |
| 6 | + |
| 7 | +* Purpose of this talk |
| 8 | +- Write unit test in golang |
| 9 | + |
| 10 | +* Why unit test? |
| 11 | +- Get computers to check our work |
| 12 | +- Run them automatically (every time we change something) |
| 13 | +- End up with a suite of tests describing the functionality |
| 14 | +- Bold and robust refactoring |
| 15 | +- We know if we break something by mistake |
| 16 | +- We know what we've broken |
| 17 | +- We all do manual testing - takes time and effort >> make less effort |
| 18 | + |
| 19 | +* Test |
| 20 | + |
| 21 | +.play -edit test1.go /START OMIT/,/END OMIT/ |
| 22 | + |
| 23 | +* Table tests |
| 24 | + |
| 25 | +.play -edit test2.go /START OMIT/,/END OMIT/ |
| 26 | + |
| 27 | +* Go test |
| 28 | + |
| 29 | +- go test - runs tests and gives you output |
| 30 | +- go test ./... - run tests for multi-package projects |
| 31 | +- go test -cover |
| 32 | + |
| 33 | +* T |
| 34 | + |
| 35 | +The `*testing.T` argument is used for error reporting: |
| 36 | + |
| 37 | + t.Errorf("got bar = %v, want %v", got, want) |
| 38 | + t.Fatalf("Frobnicate(%v) returned error: %v", arg, err) |
| 39 | + t.Logf("iteration %v", i) |
| 40 | + |
| 41 | +And enabling parallel tests: |
| 42 | + |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | +And controlling whether a test runs at all: |
| 46 | + |
| 47 | + if runtime.GOARCH == "arm" { |
| 48 | + t.Skip("this doesn't work on ARM") |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +* Test coverage |
| 53 | + |
| 54 | +The `go` tool can report test coverage statistics. |
| 55 | + |
| 56 | + $ go test -cover |
| 57 | + PASS |
| 58 | + coverage: 96.4% of statements |
| 59 | + ok strings 0.692s |
| 60 | + |
| 61 | +The `go` tool can generate coverage profiles that may be intepreted by the `cover` tool. |
| 62 | + |
| 63 | + $ go test -coverprofile=cover.out |
| 64 | + $ go tool cover -func=cover.out |
| 65 | + strings/reader.go: Len 66.7% |
| 66 | + strings/strings.go: TrimSuffix 100.0% |
| 67 | + ... many lines omitted ... |
| 68 | + strings/strings.go: Replace 100.0% |
| 69 | + strings/strings.go: EqualFold 100.0% |
| 70 | + total: (statements) 96.4% |
| 71 | + |
| 72 | +* Coverage visualization |
| 73 | + |
| 74 | + $ go tool cover -html=cover.out |
| 75 | + |
| 76 | +.image cover.png |
| 77 | + |
| 78 | +* Setup and teardown |
| 79 | +.play -edit setupteardown.go |
| 80 | + |
| 81 | + |
| 82 | +* Test Main |
| 83 | +.play -edit testmain.go |
| 84 | + |
| 85 | + |
| 86 | +* Testing HTTP clients and servers |
| 87 | + |
| 88 | +The `net/http/httptest` package provides helpers for testing code that makes or serves HTTP requests. |
| 89 | + |
| 90 | + |
| 91 | +* httptest.Server |
| 92 | + |
| 93 | +An `httptest.Server` listens on a system-chosen port on the local loopback interface, for use in end-to-end HTTP tests. |
| 94 | + |
| 95 | + type Server struct { |
| 96 | + URL string // base URL of form http://ipaddr:port with no trailing slash |
| 97 | + Listener net.Listener |
| 98 | + |
| 99 | + // TLS is the optional TLS configuration, populated with a new config |
| 100 | + // after TLS is started. If set on an unstarted server before StartTLS |
| 101 | + // is called, existing fields are copied into the new config. |
| 102 | + TLS *tls.Config |
| 103 | + |
| 104 | + // Config may be changed after calling NewUnstartedServer and |
| 105 | + // before Start or StartTLS. |
| 106 | + Config *http.Server |
| 107 | + } |
| 108 | + |
| 109 | + func NewServer(handler http.Handler) *Server |
| 110 | + |
| 111 | + func (*Server) Close() error |
| 112 | + |
| 113 | +* httptest.Server in action |
| 114 | + |
| 115 | +This code sets up a temporary HTTP server that serves a simple "Hello" response. |
| 116 | + |
| 117 | +.play httpserver.go /START OMIT/,/STOP OMIT/ |
| 118 | + |
| 119 | + |
| 120 | +* httptest.ResponseRecorder |
| 121 | + |
| 122 | +`httptest.ResponseRecorder` is an implementation of `http.ResponseWriter` that records its mutations for later inspection in tests. |
| 123 | + |
| 124 | + type ResponseRecorder struct { |
| 125 | + Code int // the HTTP response code from WriteHeader |
| 126 | + HeaderMap http.Header // the HTTP response headers |
| 127 | + Body *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to |
| 128 | + Flushed bool |
| 129 | + } |
| 130 | + |
| 131 | +* httptest.ResponseRecorder in action |
| 132 | + |
| 133 | +By passing a `ResponseRecorder` into an HTTP handler we can inspect the generated response. |
| 134 | + |
| 135 | +.play httprecorder.go /START OMIT/,/STOP OMIT/ |
| 136 | + |
| 137 | +* FE |
| 138 | +.play table_test2.go /START OMIT/,/END OMIT/ |
| 139 | + |
| 140 | +* Race Detection |
| 141 | + |
| 142 | +A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write. |
| 143 | + |
| 144 | +To help diagnose such bugs, Go includes a built-in data race detector. |
| 145 | + |
| 146 | +Pass the `-race` flag to the go tool to enable the race detector: |
| 147 | + |
| 148 | + $ go test -race mypkg // to test the package |
| 149 | + $ go run -race mysrc.go // to run the source file |
| 150 | + $ go build -race mycmd // to build the command |
| 151 | + $ go install -race mypkg // to install the package |
0 commit comments