英文:
how to use TestMain with global aetest.NewInstance
问题
我正在使用"google.golang.org/appengine/aetest"包,并设置我的TestMain如下:
var myAeInst aetest.Instance
func TestMain(m *testing.M) {
var err error
myAeInst, err = aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
defer tearDown()
c := m.Run()
os.Exit(code)
}
func tearDown() {
if myAeInst != nil {
myAeInst.Close()
}
}
但是它在aetest.NewInstance这一行卡住了,有人遇到类似的问题吗?
英文:
I am using
"google.golang.org/appengine/aetest"
package and setup my TestMain like this:
var myAeInst aetest.Instance
func TestMain(m *testing.M) {
var err error
myAeInst, err = aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
defer tearDown()
c := m.Run()
os.Exit(code)
}
func tearDown() {
if myAeInst != nil {
myAeInst.Close()
}
}
But it stuck at aetest.NewInstance, any one encounter similar issue?
答案1
得分: 1
你正在调用defer tearDown()
,然后调用os.Exit(code)
,这会在os.Exit
之后调用tearDown
(也就是说,永远不会调用)。你需要在os.Exit
之前显式地调用tearDown
,或者创建一个新的函数,在该函数中使用延迟调用,但不调用os.Exit
。
英文:
You're calling defer tearDown()
and then os.Exit(code)
, which calls tearDown
after os.Exit
(i.e., never). You need to either explicitly call tearDown
before os.Exit
, or make a new function that you defer from that doesn't call os.Exit
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论