debugging 使用VSCode调试特定的golang单元测试

ncgqoxb0  于 2023-01-21  发布在  Vscode
关注(0)|答案(2)|浏览(206)

我正在尝试使用断点调试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)`

我查看了以下资源:

  1. Debugging Go tests in Visual Studio Code
  2. https://github.com/golang/vscode-go/blob/master/docs/debugging.md
    上面列表中的#1看起来几乎完美,但我无法让它工作(我不想调试${file},我希望无论打开什么文件都运行相同的单元测试)。
    如何正确设置此选项?
niwlg2el

niwlg2el1#

修改launch.json中的"mode"属性:

"mode": "test",  // This should be "auto" or "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"
            ]
        }
    ]
}
arknldoa

arknldoa2#

我还想运行一些特定的测试,我通过设置参数“-test.run =* *"使它为我工作,因此您可以尝试:

{
    "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
}

相关问题