go test flag: flag provided but not defined

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

go test flag: flag provided but not defined

问题

你好,我正在使用一个标志(flag)来进行Go语言的测试:<br>
file_test.go
var ip = flag.String("ip", "noip", "test")

我只在一个测试文件中使用这个标志,并且当只测试那个文件时,它能正常工作。但是当我运行:
go test ./... -ip 127.0.0.1时,其他所有的测试文件都会显示"flag provided but not defined"。

你遇到过这个问题吗?

谢谢。

英文:

Hi I am using a flag when testing in go:<br>
file_test.go
var ip = flag.String(&quot;ip&quot;, &quot;noip&quot;, &quot;test&quot;)

I am only using this in one test file. And it works fine when only testing that one test file, but when I run:
go test ./... -ip 127.0.0.1 alle of the other test file saying: flag provided but not defined.

Have you seen this?

Regards

答案1

得分: 35

flag.Parse() 在定义标志之前被调用。

你必须确保在调用 flag.Parse() 之前进行所有标志的定义,通常是通过在 init() 函数中定义所有标志。

英文:

flag.Parse() is being called before your flag is defined.

You have to make sure that all flag definitions happen before calling flag.Parse(), usually by defining all flags inside init() functions.

答案2

得分: 15

如果你已经迁移到golang 13,它改变了测试初始化器的顺序,可能会导致类似于“flag provided but not defined: -test.timeout”的问题。作为一种可能的解决方法,你可以使用以下代码段:

var _ = func() bool {
    testing.Init()
    return true
}()

这将在应用程序初始化之前调用测试初始化。更多信息可以在原始线程中找到:

https://github.com/golang/go/issues/31859#issuecomment-489889428

英文:

If you've migrated to golang 13, it changed the order of the test initializer,
so it could lead to something like

flag provided but not defined: -test.timeout

as a possible workaround, you can use

var _ = func() bool {
	testing.Init()
	return true
}()

that would call test initialization before the application one. More info can be found on the original thread:

https://github.com/golang/go/issues/31859#issuecomment-489889428

答案3

得分: 3

不要在任何init()函数中调用flag.Parse()。

英文:

do not call flag.Parse() in any init()

答案4

得分: 3

我非常晚才参加这个聚会;但是在 Go 1.19.5 上又出现了这个问题吗?

我遇到了这个帖子中报告的相同错误,并且上面报告的解决方案(https://github.com/golang/go/issues/31859#issuecomment-489889428)可以修复它。

我在 v1.18go_test.go 中看到了对 flags.Parse() 的调用。

https://go.googlesource.com/go/+/f7248f05946c1804b5519d0b3eb0db054dc9c5d6%5E%21/src/cmd/go/go_test.go

我刚刚开始学习 Go,所以在我将此问题报告给其他人之前,希望有一些来自更有经验的人的验证。

英文:

I'm very late to the party; but is this broken (again) on Go 1.19.5?

I hit the same errors reported on this thread and the same solution reported above (https://github.com/golang/go/issues/31859#issuecomment-489889428) fixes it.

I see a call to flags.Parse() was added back in go_test.go in v1.18

https://go.googlesource.com/go/+/f7248f05946c1804b5519d0b3eb0db054dc9c5d6%5E%21/src/cmd/go/go_test.go

I am only just learning Go so it'd be nice to have some verification from people more skilled before I report this elsewhere.

答案5

得分: 1

如果你在通过docker-compose运行命令时遇到这个问题,那么你的引号使用不正确。例如:

services:
  app:
    ...
    image: kumina/openvpn-exporter:latest
    command: [  
      &quot;--openvpn.status_paths&quot;, &quot;/etc/openvpn_exporter/openvpn-status.log&quot;,
      &quot;--openvpn.status_paths /etc/openvpn_exporter/openvpn-status.log&quot;,
    ]

第一行是正确的,第二行是错误的,因为整行被视为一个参数。你需要通过传递两个单独的字符串来分割它们,就像第一行那样。

英文:

If you get this, when running command via docker-compose then you do incorrect quoting. Eg.

services:
  app:
    ...
    image: kumina/openvpn-exporter:latest
    command: [  
      &quot;--openvpn.status_paths&quot;, &quot;/etc/openvpn_exporter/openvpn-status.log&quot;,
      &quot;--openvpn.status_paths /etc/openvpn_exporter/openvpn-status.log&quot;,
    ]

First is correct, second is wrong, because whole line counted as one parameter. You need to split them by passing two separate strings, like in first line.

答案6

得分: 0

你只需要运行一次flag.Parse(),检查是否在调用另一段代码时使用了flag.Parse()。

英文:

You can run flag.Parse() only one time, check if you are calling another code with flag.Parse()

huangapple
  • 本文由 发表于 2015年4月17日 21:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/29699982.html
匿名

发表评论

匿名网友

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

确定