19

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

4

2 回答 2

27

使用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 文件供您清理。

于 2017-04-14T01:06:42.820 回答
1

我不熟悉 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。

于 2017-04-12T23:29:20.023 回答