英文:
Debugging a specific golang unit test with VSCode
问题
我正在尝试在VSCode中使用断点调试特定的单元测试。
在命令行中,以下命令可以完美执行:
go test -v ./cmd/bacalhau -run TestCommands
然而,当我选择以下内容运行时,出现以下问题:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run",
"TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
输出:
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:43095 --log-dest=3 from /home/user/code/bacalhau
DAP server listening at: 127.0.0.1:43095
2022-03-12T19:31:11Z info layer=debugger launching process with args: [/home/user/code/bacalhau/__debug_bin -test.run TestCommands]
2022-03-12T19:31:11Z error layer=debugger can't find build-id note on binary
Type 'dlv help' for list of commands.
2022-03-12T19:31:12Z debug layer=debugger continuing
19h31m12.78 DEBUG runtime/proc.go:6498 Log level from LOG_LEVEL_ENV_VAR: debug
Zap log level: debug
19h31m12.82 DEBUG bacalhau/main.go:12 Top of execution - 2022-03-12 19:31:12.824219499 +0000 UTC
Error: unknown command "TestCommands" for "bacalhau"
Run 'bacalhau --help' for usage.
unknown command "TestCommands" for "bacalhau"
Process 3552701 has exited with status 1
Detaching
2022-03-12T19:31:12Z debug layer=debugger detaching
dlv dap (3552580) exited with code: 0
我还尝试设置"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go"
,结果如下:
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:33479 --log-dest=3 from /home/user/code/bacalhau/cmd/bacalhau
DAP server listening at: 127.0.0.1:33479
Build Error: go build -o /home/user/code/bacalhau/cmd/bacalhau/__debug_bin -gcflags all=-N -l ./devstack_test.go
no packages to build (exit status 1)`
<details>
<summary>英文:</summary>
I am trying to debug one specific unit test in VSCode with breakpoints.
On the command line, this works perfectly to execute:
```bash
go test -v ./cmd/bacalhau -run TestCommands
However, when I select the below to run, I get the following:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run",
"TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
Output:
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:43095 --log-dest=3 from /home/user/code/bacalhau
DAP server listening at: 127.0.0.1:43095
2022-03-12T19:31:11Z info layer=debugger launching process with args: [/home/user/code/bacalhau/__debug_bin -test.run TestCommands]
2022-03-12T19:31:11Z error layer=debugger can't find build-id note on binary
Type 'dlv help' for list of commands.
2022-03-12T19:31:12Z debug layer=debugger continuing
19h31m12.78 DEBUG runtime/proc.go:6498 Log level from LOG_LEVEL_ENV_VAR: debug
Zap log level: debug
19h31m12.82 DEBUG bacalhau/main.go:12 Top of execution - 2022-03-12 19:31:12.824219499 +0000 UTC
Error: unknown command "TestCommands" for "bacalhau"
Run 'bacalhau --help' for usage.
unknown command "TestCommands" for "bacalhau"
Process 3552701 has exited with status 1
Detaching
2022-03-12T19:31:12Z debug layer=debugger detaching
dlv dap (3552580) exited with code: 0
I have also tried setting "program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
which results in
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:33479 --log-dest=3 from /home/user/code/bacalhau/cmd/bacalhau
DAP server listening at: 127.0.0.1:33479
Build Error: go build -o /home/user/code/bacalhau/cmd/bacalhau/__debug_bin -gcflags all=-N -l ./devstack_test.go
no packages to build (exit status 1)`
I have looked at the following resources:
- https://stackoverflow.com/questions/43092364/debugging-go-tests-in-visual-studio-code
- https://github.com/golang/vscode-go/blob/master/docs/debugging.md
#1 in the above list looks almost perfect, but I could not get it to work (I don't want to debug ${file}
I want the same unit test to run no matter what file I have open).
How do I set this up correctly?
答案1
得分: 4
在你的launch.json
中修改"mode"
属性:
"mode": "test", // 这里应该是 "auto" 或者 "test"
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [],
或者,如果你只想自动执行测试,你可以将其配置为一个任务,这样更简单:
{
"version": "2.0.0",
"tasks": [
{
"label": "tmp test",
"type": "shell",
"command": "go test -v ./cmd/bacalhau -run TestCommands",
"problemMatcher": [
"$go"
]
}
]
}
英文:
Modify the "mode"
property in your launch.json
:
"mode": "test", // This should be "auto" or "test"
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [],
Or, if you only want to execute the test automatically, you can config it as a task, it's simpler:
{
"version": "2.0.0",
"tasks": [
{
"label": "tmp test",
"type": "shell",
"command": "go test -v ./cmd/bacalhau -run TestCommands",
"problemMatcher": [
"$go"
]
}
]
}
答案2
得分: 1
我也想运行一些特定的测试,我通过设置参数"-test.run=
{
"name": "调试特定测试",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run=TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
英文:
I also wanted to run just some specific tests, and I got it working for me by setting the argument "-test.run=<regexp>", so you could try:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run=TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论