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

标签 performance unit-testing google-app-engine optimization 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()
    ...

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

enter image description here

重复 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/

相关文章:

objective-c - 最优解 : Get a random sample of items from a data set

MySQL 服务器负载非常高

简单套接字服务器的 Ruby 单元测试

objective-c - OCUnit 测试在未测试的类中给出错误

php - 为什么我从闭包中抛出的异常没有被捕获?

python - Google App Engine 中的进度条

java - 具有相同类型子项的 App Engine 类

c++ - 为什么标准 R 中值函数比简单的 C++ 替代函数慢得多?

php - 如何提高此数据分析的速度?

google-app-engine - Google App Engine 中的查询/数据存储出现问题 - Go API