how to use TestMain with global aetest.NewInstance

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年1月29日 14:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/35078143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定