Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


A common place to drop into a debugger is right in the middle of a unit test. This allows one to quickly iterate in a TDD fashion. With Delve there is a unique sub-command for starting a debugging session with tests running: dlv test. After executing this command, you break at the start of a specific unit test by issuing a break <TestName> statement. Finally, if you don't know the name of the test (or don't want to stop dlv to find it), you can issue the funcs command with a regex expression to list the tests of similar names. For example, here is a snippet of dropping into a dlv test session and breaking on a test in the directory:

Code Block
$ pwd
$ dlv test              
Type 'help' for list of commands.
(dlv) funcs auditlog.Test*
(dlv) break TestLogFailedQuery
Breakpoint 1 set at 0x1ce06bb for ./middleware_test.go:83
(dlv) c
> ./middleware_test.go:83 (hits goroutine(78):1 total:1) (PC: 0x1ce06bb)
    78:                 require.Equal(t, nulls.NewString("{}"), entry.Response) // default is null so this confirms the second write succeeded
    79:                 require.Equal(t, nulls.NewString(""), entry.Error)
    80:         })
    81: }
=>  83: func TestLogFailedQuery(t *testing.T) {
    84:         conn, err := pop.Connect("test")
    85:         require.NoError(t, err)
    87:         // Simulate authenticated request
    88:         om, cluster := models.CreateRandomOrgUserAndCluster(t, conn)