2017-04-12 128 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文件供您清理。