2017-04-12 83 views
2

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

回答

1

我不熟悉的钻研,但如果它可以在编译的二进制工作,用-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. 

然后运行钻研的输出。

2

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