How To Debug Golang Test Functions with Delve

https://github.com/go-delve/delve

Delve is a debugger for the Go programming language

Let’s say I have a package named actuallykasi/pkg/sample/v1/api. Inside this package there is test function TestGetCurrencyHandler as follows.

199func TestGetCurrencyHandler(t *testing.T) {
200	router := mux.NewRouter()
201	tests := []struct {
202		Name         string
203		Method       string
204		URL          string
205		WantCode     int
206		WantResponse []byte
207	}{
208		{
209			Name:         "InvalidCurrency",
210			URL:          "/currency",
211			WantCode:     http.StatusInternalServer,
212			WantResponse: errorJSON(""),
213		},
214	}
215
216	for _, tt := range tests {
217		t.Run(tt.Name, func(t *testing.T) {
218			req := httptest.NewRequest("GET", tt.URL, nil)
219			w := httptest.NewRecorder()
220			router.ServeHTTP(w, req)
221
222			resp := w.Result()
223
224			if tt.WantCode != resp.StatusCode {
225				t.Logf("want status %d; got %d", tt.WantCode, resp.StatusCode)
226			}
227
228			got, err := ioutil.ReadAll(resp.Body)
229			if err != nil {
230				t.Errorf("couldn't read body: %v", err)
231			}
232			defer resp.Body.Close()
233
234			if diff := cmp.Diff(string(tt.WantResponse), string(got)); diff != "" {
235				t.Errorf(diffMessage, "GetCurrencyHandler", diff)
236			}
237		})
238	}
239}

I want to debug why this testcase fails. actuallykasi/pkg/sample/v1/api calls a DAL function in actuallykasi/pkg/sample/storage, So my goal is to attach a breakpoint and to inspect if GetCurrency() returns correct response to api layer.

10currency, err := storage.GetCurrency()
11if err != nil {
12    //
13}
14log.Println("There was no error")

Normally I run my single testcase this way.

$ go test -v actuallykasi/pkg/sample/v1/api -run TestGetCurrencyHandler

To run it with delve debugger

$ dlv test --build-flags='actuallykasi/pkg/sample/v1/api' -- -test.run TestGetCurrencyHandler
Type 'help' for list of commands.
(dlv)

Create a breakpoint at line 14.

(dlv) break /Users/kasi/repos/go/src/actuallykasi/pkg/sample/storage:14
Breakpoint 1 set at 0x177ecd4 for actuallykasi/pkg/sample/storage.(*DB).GetCurrency() ./storage.go:14

Allow program to run

(dlv) continue
> actuallykasi/pkg/sample/storage.(*DB).GetCurrency() ./storage.go:14 (hits goroutine(12):1 total:1) (PC: 0x177ecd4)
    10:     currency, err := storage.GetCurrency()
    11:     if err != nil {
    12:         //
    13:     }
=>  14:     log.Println("There was no error")
    15:     // continue.
    16:     //

Inspect variable currency

(dlv) print currency
Command failed: currency is nil

So we conclude GetCurrency() returns nil for some reason. Debugging continues.