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

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

How to speed up Google App Engine Go unit tests?

问题

我目前正在为在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并将其传递下去,还是因为我为每个单元测试使用了单独的Suite?如何加快这个过程?

英文:

I am currently writing a lot of unit tests for my package that runs on GAE Go. The package in question is focused on data saving and loading to and from appengine/datastore. As such, I have about 20 unit test files that look a bit like this:

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()
    ...

As a result, each individual test file appears to be starting its own version of devappserver:

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

Repeat this 20 times and my unit tests run for over 10 minutes.

I am wondering, how can I speed up the execution of my testing suite? Should I have just one file that creates aetest.NewContext and passes that onwards, or is it due to me using separate Suites for each unit test? How can I speed this thing up?

答案1

得分: 4

你可以使用自定义的TestMain函数:

var ctx aetest.Context

func TestMain(m *testing.M) {
    var err error
    ctx, err = aetest.NewContext(nil)
    if err != nil {
        panic(err)
    }
    code := m.Run() // 这里运行测试
    ctx.Close()
    os.Exit(code)
}

func TestUsers(t *testing.T) {
    // 在这里使用 ctx
}

这样,开发服务器将在所有测试中只启动一次。有关TestMain的更多详细信息,请参阅此处:http://golang.org/pkg/testing/#hdr-Main

英文:

You can use a custom TestMain function:

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
}

This way the dev server is started once for all the tests. More details on TestMain are available here: http://golang.org/pkg/testing/#hdr-Main.

huangapple
  • 本文由 发表于 2015年3月4日 08:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/28844384.html
匿名

发表评论

匿名网友

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

确定