- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想运行一些测试用例,需要启动 GRPC 模拟服务器。为此,我正在使用 gomock
库。要启动服务器,我必须将类型为 testing.T
的变量传递给此函数 - gomock.NewController()
.由于这是对所有测试用例的一种初始化,我想在 TestMain
中执行此操作。但是 TestMain
只能访问 testing.M
那么我该如何处理这种情况呢?在 TestMain
中创建一个新的 testing.T
结构?会成功吗?
最佳答案
听起来您正在寻找 BeforeEach
模式。您无权访问 TestMain
中的 testing.T
对象,因为这是在测试套件运行前后进行初始化的更多地方。
有一些框架可以为您提供便宜的 BeforeEach
:
仅举几例。
你也可以自己手卷:
type test struct{
ctrl *gomock.Controller
mockFoo *MockFoo
// ...
}
func beforeEach(t *testing.T) test {
ctrl := gomock.NewController(t)
return test {
ctrl:ctrl,
mockFoo: NewMockFoo(ctrl),
}
}
func TestBar(t *testing.T) {
test := beforeEach(t)
// ...
}
func TestBaz(t *testing.T) {
test := beforeEach(t)
// ...
}
关于go - 在 TestMain 中使用 testing.T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53129303/
我正在编写一个编译 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
我是一名优秀的程序员,十分优秀!