- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
似乎我的 func TestMain 没有运行
我的测试文件夹 /_test
中有 2 个文件。一个是/main_test.go
,另一个是/api_city_find_all_test.go
。
我运行第二个,出现 panic 错误,说我得到了 nil 指针运行
谁能帮我解决这个问题?
main_test.go
func TestMain(m *testing.M) {
fmt.Print("TEST@@")
os.Chdir("../../../")
boot.Bootstrap()
rajaongkir.Register()
os.Exit(m.Run())
}
我跑
api_city_find_all_test.go
func TestApiCityFindAllTest(t *testing.T) {
goreq := libraries.Request("http://localhost:8181").Post([]string{"rajaongkir/city/findAll"}, nil, `{}`)
req, _ := goreq.MakeRequest()
resp := httptest.NewRecorder()
fmt.Print("TEST!!")
utilities.Globals.Router.ServeHTTP(resp, req)
respBody, _ := ioutil.ReadAll(resp.Body)
t.Log(string(respBody))
assert.NotEqual(t, "null", string(respBody))
}
我期待“测试!!”在 api_rajaongkir_city_find_all_test.go 和 main_test.go 中的“TEST@@”被打印但只是“TEST!!”显示出来,说明main_Test.go没有运行
最佳答案
TestMain
函数是测试包的本地函数。
If a test file contains a function:
func TestMain(m *testing.M)
then the generated test will call
TestMain(m)
instead of running the tests directly.
您必须确保两个文件定义相同的包或在两个文件中定义 TestMain(m)
。
关于go - 函数 TestMain 没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56054284/
我正在编写一个编译 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
我是一名优秀的程序员,十分优秀!