gpt4 book ai didi

go - 在多个实例上添加 testmain 前缀

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:16 25 4
gpt4 key购买 nike

我正在使用 TestMain 测试同一接口(interface)的多个实现

func TestMain(m *testing.M) {
setup1()
code = m.Run()
if code != 0 {
os.exit(code)
}

setup2()
code = m.Run()
if code != 0 {
os.exit(code)
}
}

如果我在测试中遇到错误,就很难知道是哪个实现导致了失败

在 T 子测试中,你像这样运行:

t.run("test name", testfunc)

有没有办法为主要测试添加前缀

m.Run("name") // intuitively what I should be able to do

编辑:添加一些背景,因为看起来人们没有看到它是关于什么的

// MyInterface is implemented multiple time, but we expect the same behavior for any implementation
type IMyInterface interface {
SomeMethod()
SomeOtherMethod()
}

var (
implUnderTest IMyInterface // is referred to in every tests
)

// There are many test files with many tests run for each implementation
func TestMain(m *testing.M) {
for _, impl := range []IMyInterface {&Impl1{}, &Impl2{}, &Impl3{}} {
implUnderTest = impl
code := m.Run()
if code != 0 {
os.exit(code)
}
}

}

最佳答案

多次调用 m.Run() 似乎是一种反模式。创建具有不同设置的多个测试并相应地命名它们。据我所知,没有 m.Run("name")

关于go - 在多个实例上添加 testmain 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com