英文:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论