我需要在Go中构建一个测试用例,在执行时接受一些命令行参数。

测试文件看起来非常简单:

package somelogic_test

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

func TestSomeLogic(t *testing.T) {
    flag.Parse()
    strSlice := flag.Args()
    sl.SomeLogic(strSlice)
}
go test -v somelogic_test.go -args arg1 arg2 arg3

现在我想在VSCode中调试执行。我发现这个链接建议将所有命令行参数放在launch.json文件的“args”字段中。所以我按照建议做了,我的launch.json配置现在如下所示:

{
    "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"]
        }
    ]
}
flag.Parse()flag.Args()strSlice
flag.Parse()