Andrii's blog Blog about programming

Debugging Golang Tests With Delve

How to debug golang unit-tests using delve 🐛🔥?

So, often you need to run 1 test and even in debug mode, for example, when you wrote a test that repeats a bug. It’s very simple (although not very obvious from the docks):

dlv test -- NameOfYourTest/PartOfTheName* 

similar to go test -run.

Or a live example:

➜ debug_test dlv test -- TestFibonacciBig
(dlv) b main_test.go:6
Breakpoint 1 set at 0x115887f for ./main_test.go:6
(dlv) c
> ./main_test.go:6 (hits goroutine(17):1 total:1) (PC: 0x115887f)
1: package main
3: import "testing"
5: func TestFibonacciBig(t *testing.T) {
=> 6: var want int64 = 55
7: got := FibonacciBig(10)
8: if got.Int64() != want {
9: t.Errorf("Invalid Fibonacci value for N: %d, got: %d, want: %d", 10, got.Int64(), want)
10: }
11: }

Alternatively, run with -v (remember go test -v):

➜ debug_test dlv test -- -test.v TestFibonacciBig
(dlv) c
=== RUN TestFibonacciBig
--- PASS: TestFibonacciBig (0.00s)


comments powered by Disqus