使用deve调试测试

Spi*_*ico 9 debugging go delve

我正在使用"go test -v "来运行一系列单元测试.我想用delve调试它们.当我尝试运行调试器时,出现"无法调试非主程序包"错误.那么,我如何使用delve调试器调试单元测试?

Mar*_*oij 11

用途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
Run Code Online (Sandbox Code Playgroud)

您也可以传递-test.run选择要运行的测试(就像go test -run).

在内部,这与Flimzy的答案(它编译测试二进制文件go test -c)相同,但更加简化,不会留下.test文件供您清理.