I'm using "go test -v" to run bunch of unit tests. I'd like to debug them using delve. When I try to run debugger, I get an "Can not debug non-main package" error. So, how can I debug unit tests using delve debugger ?
Asked
Active
Viewed 1.7k times
2 Answers
35
Use dlv test
:
$ dlv test -- -test.v
Type 'help' for list of commands.
(dlv) continue
=== RUN TestReadFileError
--- PASS: TestReadFileError (0.00s)
=== RUN TestReadFile
--- PASS: TestReadFile (0.00s)
[..]
PASS
Process 8014 has exited with status 0
(dlv) quit
Process 8014 has exited with status 0
You can also pass -test.run
to select tests to run (just like go test -run
).
Internally, this is the same as Flimzy's answer (it compiles the test binary with go test -c
), but more streamlined and won't leave .test files for you to clean up.

Matthew
- 3,510
- 2
- 23
- 24

Martin Tournoij
- 26,737
- 24
- 105
- 146
-
Where is `-test.run` documented? It's not in https://github.com/derekparker/delve/blob/master/Documentation/usage/dlv_test.md – jcollum May 13 '20 at 17:58
-
1It's a flag for the binary compiled with `go test`, not delve @jcollum – Martin Tournoij May 14 '20 at 03:02
1
I'm not familiar with delve, but if it can work on a compiled binary, just compile your tests using the -c
flag:
-c
Compile the test binary to pkg.test but do not run it
(where pkg is the last element of the package's import path).
The file name can be changed with the -o flag.
Then run delve on the output.

Jonathan Hall
- 75,165
- 16
- 143
- 189
-
1I got this to work dlv exec ./pkg.test -- -gocheck.f TestLoadImage but Type 'help' for list of commands. (dlv) step Stopped at: 0x7fe7886b2090 => 1: no source available Command failed: no source for PC 0x7fe7886b2090 How do you indicate where the source code should be? – nicocesar Mar 24 '21 at 13:49