0

I'm running Golang tests on VSCode with some global variables, that being initialized before reaching the test function.

The global variables in my program get their values from runline arguments, that specified in the launch.json file (os.Args).

For some reason when I'm running tests I have different args than the ones specified in the launch.json file.

These are contents of my launch file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}",
            "args": [".", "from", "to", "csv", "hl7"]
        }
    ]
}

These are the different values that I constantly get:

os.Args[0] = //path to the __debug_bin.exe
os.Args[1] = "-test.run"
os.Args[2] = "^TestInit$"

In my launch.json file I have 6 arguments in total, which leads me to an out of bounds error when I try to run tests.

So my question is how do change it so that tests run with the arguments I specified in the launch file?

  • 1
    Does this help: https://github.com/microsoft/vscode-go/issues/2115 – marc wellman May 30 '22 at 06:57
  • It doesn't seem to help, I still get those constant args I mentioned. Is it possible that there is a default launch.json file specifically for go tests in VSCode? Because it seems that os.Args is taking values from somewhere, I just don't know where – Zedi Zedi May 30 '22 at 08:20
  • Did you try to manually run the test with for example `go test main_test.go`? Would you be able to provide and access arguments as expected? – marc wellman May 30 '22 at 09:11
  • Also, does setting the `mode` in your launch.json to either `test` or `debug` make any difference? – marc wellman May 30 '22 at 09:20
  • Unfortunately no, for both your answers. running go test with the file itself seem to ignore the globals, but I need them initialized and the test/debug doesn't effect anything (i still get the same default launch.json file) – Zedi Zedi May 30 '22 at 09:45

1 Answers1

0

codelens does not support this feature for now please check here codelens: a way to use selected launch.json configuration in run test | debug

But you can do like this

  1. setup "go.testFlags": ["-v", "-args", "from", "to", "csv", "hl7"] in user setting (path for file) "Code\User\settings.json"

and you can check like this

t.Log(os.Args[3])
t.Log(os.Args[4])

....

Manjeet Thakur
  • 2,288
  • 1
  • 16
  • 35