使用 delve 调试测试
Debugging tests with delve
我正在使用 "go test -v" 到 运行 一堆单元测试。我想使用 delve 调试它们。当我尝试 运行 调试器时,出现 "Can not debug non-main package" 错误。那么,如何使用 delve 调试器调试单元测试?
我不熟悉 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.
然后运行深入研究输出。
使用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
到 select 测试传递给 运行(就像 go test -run
)。
在内部,这与 Flimzy 的答案相同(它使用 go test -c
编译测试二进制文件),但更精简并且不会留下 .test 文件供您清理。
我正在使用 "go test -v" 到 运行 一堆单元测试。我想使用 delve 调试它们。当我尝试 运行 调试器时,出现 "Can not debug non-main package" 错误。那么,如何使用 delve 调试器调试单元测试?
我不熟悉 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.
然后运行深入研究输出。
使用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
到 select 测试传递给 运行(就像 go test -run
)。
在内部,这与 Flimzy 的答案相同(它使用 go test -c
编译测试二进制文件),但更精简并且不会留下 .test 文件供您清理。