- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如果我定义两个测试,每个测试都有自己的 TestMain
方法,go test
错误:“找到多个 TestMain 定义”
。
我能理解并期待这种行为,因为在同一个包中不应该有多个 TestMain。但是,我不知道现在该怎么办。每个测试套件都有自己的需求。我需要创建不同的 TestMain
来设置测试,当然,无需重命名我的包。
我可以在其他语言中使用 before
、after
等设置方法轻松做到这一点,这是测试类所独有的。
我可能会去使用 testify的套房。遗憾的是,stdlib 不支持这一点。
你有什么建议吗?
最佳答案
您可以使用 M.Run .
func TestMain(m *testing.M) {
setup()
code := m.Run()
close()
os.Exit(code)
}
参见 subtest了解更多信息。
更详细的例子:
package main
import (
"testing"
)
func setup() {}
func teardown() {}
func setup2() {}
func teardown2() {}
func TestMain(m *testing.M) {
var wrappers = []struct {
Setup func()
Teardown func()
}{
{
Setup: setup,
Teardown: teardown,
},
{
Setup: setup2,
Teardown: teardown2,
},
}
for _, w := range wrappers {
w.Setup()
code := m.Run()
w.Teardown()
if code != 0 {
panic("code insn't null")
}
}
}
关于testing - 找到 TestMain 多个定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552393/
我正在编写一个编译 C 源文件并将输出写入另一个文件的包。我正在为这个包编写测试,我需要创建一个临时目录来写入输出文件。我正在使用 TestMain 函数来执行此操作。出于某种原因,当我刚刚运行 Te
我在 go 中有一个测试包,它测试一些依赖于读取配置的东西。我想在运行所有测试之前读取该配置一次,所以我尝试使用 TestMain(m *testing.M): main.go: package te
我正在我的包 programapper 中创建 Java 文件,并且在每个文件的顶部都有 package programapper; 。其中一个文件名为 TestMain.java: package
我已经更新了问题以避免被指责发布 XY 问题。 之前的问题是: how can I tell when runtime.Goexit has been called on the main gorou
我正在使用 TestMain 测试同一接口(interface)的多个实现 func TestMain(m *testing.M) { setup1() code = m.Run()
我有以下 TestMain 函数: func TestMain(m *testing.M) { db := "[working_db_connection]" dbInstance, _ :=
似乎我的 func TestMain 没有运行 我的测试文件夹 /_test 中有 2 个文件。一个是/main_test.go,另一个是/api_city_find_all_test.go。 我运行
如果我定义两个测试,每个测试都有自己的 TestMain 方法,go test 错误:“找到多个 TestMain 定义”。 我能理解并期待这种行为,因为在同一个包中不应该有多个 TestMain。但
我想运行一些测试用例,需要启动 GRPC 模拟服务器。为此,我正在使用 gomock 库。要启动服务器,我必须将类型为 testing.T 的变量传递给此函数 - gomock.NewControll
我有以下测试代码,用于测试一些端点和数据库 (mongoDB) 功能。我正在使用 mgo 包,每次访问数据库时我都有一些抽象来获取新的 session 副本。 package resolvers_te
我正在尝试测试我的 http Controller 并且我使用 TestMain 函数来准备我的测试,但是在我运行所有测试请求之前我需要先运行 TestAuthUserController 测试,它创
我正在使用 “google.golang.org/appengine/aetest”像这样打包和设置我的 TestMain: var myAeInst aetest.Instance func Tes
我是一名优秀的程序员,十分优秀!