Home > OS >  Debugging Go tests with command line arguments in VSCode
Debugging Go tests with command line arguments in VSCode

Time:04-09

I need to build a test case in Go that accepts some command line arguments upon execution.

Test file looks pretty plain:

package somelogic_test

import (
    sl "example.com/somelogic"
    "flag"
    "testing"
)

func TestSomeLogic(t *testing.T) {
    flag.Parse()
    strSlice := flag.Args()
    sl.SomeLogic(strSlice)
}

When I run the test as go test -v somelogic_test.go -args arg1 arg2 arg3 it works like charm, accepting arg1, arg2, arg3 as a slice of strings and passing it over to SomeLogic function. So far, so good.

Now I want to debug the execution in VSCode. I found this link that suggests putting all command line parameters in "args" field of the launch.json file. So I did as per suggestion, and my launch.json configuration now looks as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": ["-v","somelogic_test.go","-args","arg1","arg2","arg3"]
        }
    ]
}

However, this does not work at all, cause when I run somelogic_test.go both in Run Test and Debug Test modes no arguments are accepted with flag.Parse() and flag.Args(), and as a result strSlice is just an empty slice.

Please suggest how the launch.json should be modified so that command line arguments are accepted by flag.Parse() and available for further debugging?

CodePudding user response:

Refer to go help testflag:

The 'go test' command takes both flags that apply to 'go test' itself and flags that apply to the resulting test binary.

You need to differ the two kinds of flags, because when debug test in VSCode with Go extension, it compiles the test binary first then pass the arguments to it. In your go test command line, -v somelogic_test.go should be flags to go test itself, arg1 arg2 arg3 should be flags to the resulting test binary, isn't it?

Try this, which worked in my environment:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Test file",
            "type": "go",
            "request": "launch",
            "mode": "test",
            // Indicate the target here
            "program": "${fileDirname}/somelogic_test.go",
            // or just use "The current opened file"
            // "program": "${file}", 
            "env": {},
            "args": ["-test.v", "--", "arg1","arg2","arg3"]
        }
    ]
}

However, this does not work at all, cause when I run somelogic_test.go both in Run Test and Debug Test modes no arguments are accepted with flag.Parse() and flag.Args(), and as a result strSlice is just an empty slice.

Notice run test and debug test buttons appear above the test function do not use launch.json. Go to Run and debug side bar to launch the particular configuration you set.

  • Related