测试中的标志导致其他测试无法运行 [英] flag in test causes other tests to fail to run

查看:26
本文介绍了测试中的标志导致其他测试无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试文件,我只想在设置位标志时运行它.我遵循了 golang's testing docs 中的简单示例:

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())
    }
}

如果我运行 go test ./mypkg -myflag 它会按预期运行.但是,当我运行 go test ./... -myflag 时,所有其他包测试都失败了:

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?

推荐答案

当您运行 test ./... 时,它会为每个包运行测试作为单独的可执行文件, 参数相同.因此,如果您想将标志传递给每个包,则每个包都必须接受它.您可能想改用 env var,它可以被使用它的包读取,而被不使用它的包忽略.

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.

这篇关于测试中的标志导致其他测试无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆