Debugging unit tests in go

3,224 views
Skip to first unread message

parthasar...@gmail.com

unread,
Mar 8, 2018, 11:32:42 AM3/8/18
to golang-nuts
Hi,
How can I debug the unit tests in go ? Normal executible I debug with gdb or delve.
I am running from command prompt go test -v ./test/... -run Test_Connect.

Any thing better with ide ?

Thanks

Ian Lance Taylor

unread,
Mar 8, 2018, 11:46:03 AM3/8/18
to parthasar...@gmail.com, golang-nuts
On Thu, Mar 8, 2018 at 4:41 AM, <parthasar...@gmail.com> wrote:
>
> How can I debug the unit tests in go ? Normal executible I debug with gdb or
> delve.
> I am running from command prompt go test -v ./test/... -run Test_Connect.

Run `go -test c PKG` to get an executable for the package PKG. You
can then run that executable with -test.v and test.run options.

Ian

ralphdo...@gmail.com

unread,
Mar 8, 2018, 2:46:54 PM3/8/18
to golang-nuts
I use delve for unit tests:

$ ~/go/bin/dlv test
Type 'help' for list of commands.
(dlv) b eth_test.go:49
Breakpoint 1 set at 0x5691fb for github.com/nerdralph/crypto/sha3.makeCacheFast() ./eth_test.go:49
(dlv) c
> github.com/nerdralph/crypto/sha3.makeCacheFast() ./eth_test.go:49 (hits goroutine(1):1 total:1) (PC: 0x5691fb)
 

Sotirios Mantziaris

unread,
Mar 9, 2018, 2:06:59 AM3/9/18
to golang-nuts
Visual studio code with go extension which uses delve. Just works like a charm.

Jason E. Aten

unread,
Mar 9, 2018, 7:10:59 PM3/9/18
to golang-nuts
This is not meant to be snide or flippant. Go actually made me learn Printf based debugging, and I am a much better debugger for it.

Add fmt.Printf() to your code. Or use a shortcut function like PP(), which will automatically show where it was called from to. I use
a file like vprint.go in all my projects:


You'll thank me when you get to multiple go routines. Printf restores sanity to debugging, and a debugger is mostly useless.

It turns out the Printf yields a much faster debugging process too.

ralphdo...@gmail.com

unread,
Mar 9, 2018, 9:12:15 PM3/9/18
to golang-nuts
I use mostly Printf-based debugging, and still find delve useful at times.  Primarily when I'm not sure exactly where in my code a bug arises, so I'll set a couple breakpoints, step through some code, and sometimes find I need to add some debug output in the problem area.  If delve had more output options (like hex dump of byte arrays), I'd probably use it even more.
Also, to avoid code bloat or performance impact, I use debug tags.
Reply all
Reply to author
Forward
0 new messages