如何加速Google App Engine Go单元测试?

The*_*chu 6 optimization performance google-app-engine unit-testing go

我目前正在为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()
    ...
Run Code Online (Sandbox Code Playgroud)

因此,每个单独的测试文件似乎都在启动自己的devappserver版本:

在此输入图像描述

重复这20次,我的单元测试运行超过10分钟.

我想知道,我怎样才能加快测试套件的执行速度?我是否应该只创建一个创建aetest.NewContext的文件并将其传递给我,或者是因为我为每个单元测试使用单独的套件?我怎样才能加速这件事呢?

Cal*_*leb 4

您可以使用自定义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
}
Run Code Online (Sandbox Code Playgroud)

这样,开发服务器就可以针对所有测试启动一次。更多详细信息TestMain请参见:http://golang.org/pkg/testing/#hdr-Main