使用VSCode调试特定的Golang单元测试

huangapple go评论81阅读模式
英文:

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:

    {
        &quot;name&quot;: &quot;Debug Specific Test&quot;,
        &quot;type&quot;: &quot;go&quot;,
        &quot;request&quot;: &quot;launch&quot;,
        &quot;mode&quot;: &quot;debug&quot;,
        &quot;program&quot;: &quot;${workspaceFolder}/cmd/bacalhau/devstack_test.go&quot;,
        &quot;args&quot;: [
            &quot;-test.run&quot;,
            &quot;TestCommands&quot;
        ],
        &quot;env&quot;: {
            &quot;LOG_LEVEL&quot;: &quot;debug&quot;
        },
        &quot;showLog&quot;: 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&#39;t find build-id note on binary
Type &#39;dlv help&#39; 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 &quot;TestCommands&quot; for &quot;bacalhau&quot;
Run &#39;bacalhau --help&#39; for usage.
unknown command &quot;TestCommands&quot; for &quot;bacalhau&quot;
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 &quot;program&quot;: &quot;${workspaceFolder}/cmd/bacalhau/devstack_test.go&quot;, 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:

  1. https://stackoverflow.com/questions/43092364/debugging-go-tests-in-visual-studio-code
  2. 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 &quot;mode&quot; property in your launch.json:

        &quot;mode&quot;: &quot;test&quot;,  // This should be &quot;auto&quot; or &quot;test&quot;
        &quot;program&quot;: &quot;${workspaceFolder}/cmd/bacalhau/devstack_test.go&quot;,
        &quot;args&quot;: [],

Or, if you only want to execute the test automatically, you can config it as a task, it's simpler:

{
    &quot;version&quot;: &quot;2.0.0&quot;,
    &quot;tasks&quot;: [
        {
            &quot;label&quot;: &quot;tmp test&quot;,
            &quot;type&quot;: &quot;shell&quot;,
            &quot;command&quot;: &quot;go test -v ./cmd/bacalhau -run TestCommands&quot;,
            &quot;problemMatcher&quot;: [
                &quot;$go&quot;
            ]
        }
    ]
}

答案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=&lt;regexp&gt;", so you could try:

{
    &quot;name&quot;: &quot;Debug Specific Test&quot;,
    &quot;type&quot;: &quot;go&quot;,
    &quot;request&quot;: &quot;launch&quot;,
    &quot;mode&quot;: &quot;debug&quot;,
    &quot;program&quot;: &quot;${workspaceFolder}/cmd/bacalhau/devstack_test.go&quot;,
    &quot;args&quot;: [
        &quot;-test.run=TestCommands&quot;
    ],
    &quot;env&quot;: {
        &quot;LOG_LEVEL&quot;: &quot;debug&quot;
    },
    &quot;showLog&quot;: true
}

huangapple
  • 本文由 发表于 2022年3月13日 03:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/71452550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定