aronchick
aronchick

Reputation: 7128

Debugging a specific golang unit test with VSCode

I am trying to debug one specific unit test in VSCode with breakpoints.

On the command line, this works perfectly to execute:

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:

  1. 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?

Upvotes: 3

Views: 6480

Answers (3)

Matt Welke
Matt Welke

Reputation: 1868

The answers provided so far show how to do this with launch configurations. If you don't want to set up a launch config, you can also do this by clicking on the test in the text explorer tab in the VS Code UI.

Example:

//main.go
package main

import "fmt"

func sum(a, b int) int {
    return 2
}

func main() {
    a := 1
    b := 2
    fmt.Printf("The sum of %d and %d is %d.\n", a, b, sum(a, b))
}
// main_test.go
package main

import "testing"

func Test_sum(t *testing.T) {
    type args struct {
        a int
        b int
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        {
            name: "1 + 1 = 2",
            args: args{
                a: 1,
                b: 1,
            },
            want: 2,
        },
        {
            name: "1 + 2 = 3",
            args: args{
                a: 1,
                b: 2,
            },
            want: 3,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := sum(tt.args.a, tt.args.b); got != tt.want {
                t.Errorf("sum() = %v, want %v", got, tt.want)
            }
        })
    }
}

If you run the tests once, the test explorer will show individual subtests (entries in the test table). There is a "debug test" button for the entire test, and individual debut test buttons for each of the subtests.

Screenshot of VS Code showing how to debug an individual test from a test table

Upvotes: 0

Stefan
Stefan

Reputation: 11

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
}

Upvotes: 1

rustyhu
rustyhu

Reputation: 2157

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

Upvotes: 6

Related Questions