【发布时间】:2017-09-08 21:11:51
【问题描述】:
我正在使用 "go test -v" 来运行一堆单元测试。我想使用 delve 调试它们。当我尝试运行调试器时,我收到 “无法调试非主包” 错误。那么,如何使用 delve 调试器调试单元测试呢?
【问题讨论】:
我正在使用 "go test -v" 来运行一堆单元测试。我想使用 delve 调试它们。当我尝试运行调试器时,我收到 “无法调试非主包” 错误。那么,如何使用 delve 调试器调试单元测试呢?
【问题讨论】:
使用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
您也可以通过-test.run 选择要运行的测试(就像go test -run)。
在内部,这与 Flimzy 的答案相同(它使用 go test -c 编译测试二进制文件),但更加精简,不会留下 .test 文件供您清理。
【讨论】:
-test.run 记录在哪里?不在github.com/derekparker/delve/blob/master/Documentation/usage/…
go test 编译的二进制文件的标志,而不是 delve @jcollum
我不熟悉 delve,但如果它可以在已编译的二进制文件上运行,只需使用 -c 标志编译您的测试:
-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.
然后在输出上运行 delve。
【讨论】: