我目前正在为在 GAE Go 上运行的包编写大量单元测试。有问题的包专注于数据保存和从 appengine/datastore 加载。因此,我有大约 20 个看起来有点像这样的单元测试文件:
package Data
import (
"appengine"
"appengine/aetest"
. "gopkg.in/check.v1"
"testing"
)
func TestUsers(t *testing.T) { TestingT(t) }
type UsersSuite struct{}
var _ = Suite(&UsersSuite{})
const UserID string = "UserID"
func (s *UsersSuite) TestSaveLoad(cc *C) {
c, err := aetest.NewContext(nil)
cc.Assert(err, IsNil)
defer c.Close()
...
因此,每个单独的测试文件似乎都在启动其自己版本的 devappserver:
重复 20 次,我的单元测试运行了 10 多分钟。
我想知道,如何才能加快测试套件的执行速度?我应该只有一个文件来创建 aetest.NewContext 并向前传递它,还是因为我为每个单元测试使用了单独的套件?我怎样才能加快这件事?
最佳答案
您可以使用自定义 TestMain
函数:
var ctx aetest.Context
var c aetest.Context
func TestMain(m *testing.M) {
var err error
ctx, err = aetest.NewContext(nil)
if err != nil {
panic(err)
}
code := m.Run() // this runs the tests
ctx.Close()
os.Exit(code)
}
func TestUsers(t *testing.T) {
// use ctx here
}
这样开发服务器就可以为所有测试启动一次。有关 TestMain
的更多详细信息,请访问此处:http://golang.org/pkg/testing/#hdr-Main .
关于performance - 如何加速 Google App Engine Go 单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844384/