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?