VS Code调试Go测试未通过标志位。

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

vs code debuging go tests not passing flags

问题

我正在尝试在VS Code上配置一个调试器来运行一些Go语言的测试。我需要传递一些标志参数,但是它没有正常工作。

main.go

package main

import (
	"flag"
	"fmt"
)

func DoTheThing() {
	flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
	flag.Parse()
	fmt.Println(*flag1Ptr)
	fmt.Println("Hello, world")
}

func main() {
	DoTheThing()
}

main_test.go

package main

import "testing"

func TestDoTheThing(t *testing.T) {
	DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": [
        "--", "-flag1"
    ]
}

如果我使用"Launch app"的配置来运行,它会正确地传递参数值,但是使用"Run Tests"的配置时,它不会设置该参数。

使用"Launch app"配置的输出结果:

true
Hello, world

使用"Run Tests"配置的输出结果:

false
Hello, world
英文:

I`m trying to config a debugger on vs-code to some tests in go. I have to pass some flags to it, but it's not working well.

main.go

package main

import (
	"flag"
	"fmt"
)

func DoTheThing() {
	flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
	flag.Parse()
	fmt.Println(*flag1Ptr)
	fmt.Println("Hello, world")
}

func main() {
	DoTheThing()
}

main_test.go

package main

import "testing"

func TestDoTheThing(t *testing.T) {
	DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": [
        "--", "-flag1"
    ]
}

if I run it with Launch app config it pass the value in the right way, but using the test one it do not set the parameter

output using Launch app config

true
Hello, world

output using Run Tests config

false
Hello, world

答案1

得分: 2

package main

import (
    "flag"
    "fmt"
)

var flag1Ptr *bool

func init(){
    flag1Ptr = flag.Bool("flag1", false, "flag1 is a flag")
}

func DoTheThing() {
    flag.Parse()
    fmt.Println(*flag1Ptr)
    fmt.Println("Hello, world")
}

func main() {
    DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": ["-flag1"]
}
英文:
package main

import (
    "flag"
    "fmt"
)

var flag1Ptr *bool

func init(){
    flag1Ptr = flag.Bool("flag1", false, "flag1 is a flag")
}

func DoTheThing() {
	flag.Parse()
    fmt.Println(*flag1Ptr)
    fmt.Println("Hello, world")
}

func main() {
    DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": ["-flag1"]
}

huangapple
  • 本文由 发表于 2022年7月19日 19:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73036345.html
匿名

发表评论

匿名网友

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

确定