使用命令行参数在VSCode中调试Go测试

huangapple go评论62阅读模式
英文:

Debugging Go tests with command line arguments in VSCode

问题

我需要在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运行测试时,它能够正常工作,将arg1、arg2、arg3作为字符串切片接受,并将其传递给SomeLogic函数。到目前为止,一切都很好。

现在我想在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"]
        }
    ]
}

然而,这根本不起作用,因为当我在运行测试和调试测试模式下运行somelogic_test.go时,flag.Parse()flag.Args()都不接受任何参数,结果strSlice只是一个空切片。

请建议如何修改launch.json,以便flag.Parse()接受命令行参数并可供进一步调试使用?

英文:

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?

答案1

得分: 1

请参考go help testflag

go test命令接受适用于go test本身的标志,也接受适用于生成的测试二进制文件的标志。

你需要区分这两种类型的标志,因为在使用Go扩展的VSCode中调试测试时,它首先编译测试二进制文件,然后将参数传递给它。在你的go test命令行中,-v somelogic_test.go应该是适用于go test本身的标志,arg1 arg2 arg3应该是适用于生成的测试二进制文件的标志,对吗?

尝试一下这个,在我的环境中有效:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Test file",
            "type": "go",
            "request": "launch",
            "mode": "test",
            // 在这里指定目标
            "program": "${fileDirname}/somelogic_test.go",
            // 或者直接使用“当前打开的文件”
            // "program": "${file}", 
            "env": {},
            "args": ["-test.v", "--", "arg1","arg2","arg3"]
        }
    ]
}

然而,这根本不起作用,因为当我在“运行测试”和“调试测试”模式下运行somelogic_test.go时,flag.Parse()flag.Args()都不接受参数,结果strSlice只是一个空切片。

请注意,出现在测试函数上方的“运行测试”和“调试测试”按钮不使用launch.json。请转到“运行和调试”侧边栏,启动您设置的特定配置。

英文:

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.

huangapple
  • 本文由 发表于 2022年4月6日 21:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71767280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定