flag in test causes other tests to fail to run

ぃ、小莉子 提交于 2020-01-06 01:44:59

问题


I have a test file that I want to only run when a bit flag is set. I followed the simple example from golang's testing docs:

package mypkg

var myFlagSet = flag.Bool("myflag", false, "")
func TestMain(m *testing.M) {
    flag.Parse()
    if *myFlagSet {
        os.Exit(m.Run())
    }
}

if I run go test ./mypkg -myflag it runs as expected. However, when I run go test ./... -myflag, all other package tests fail:

flag provided but not defined: -myflag

I want to be able to run all tests and not have to worry about parsing this flag in every test file. Is there a way to do this that I'm missing?


回答1:


When you run test ./... it runs tests for each package as a separate executable, with the same parameters. So if you want to pass a flag to every package, every package has to accept it. You might want to use an env var instead, which can be read by packages that use it and ignored by those that don't.



来源:https://stackoverflow.com/questions/49927650/flag-in-test-causes-other-tests-to-fail-to-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!