5

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?

3
  • If you are testing the SomeLogic function which takes the value of a flag (as opposed to handling flag logic), can you not just pass expected values into it? Commented Apr 6, 2022 at 13:20
  • I definitely can just hard code the strSlice and pass it to SomeLogic. However, I want some flexibility, and this is why I try to make it work with command line arguments Commented Apr 6, 2022 at 13:26
  • I don't know how to do what you're aiming to do here, but I don't think this is something that is required for this test. You don't need to be testing the flag module does it job, just that given the value of a flag the SomeLogic function does. Commented Apr 6, 2022 at 13:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

is there some way to do this with named params? I'm having problems with this here: stackoverflow.com/questions/73036345/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.