Debugging tests with delve

有些话、适合烂在心里 提交于 2020-01-12 07:29:11

问题


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 ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43380530/debugging-tests-with-delve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!