Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up Google App Engine Go unit tests?

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:

enter image description here

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?

like image 262
ThePiachu Avatar asked Mar 04 '15 00:03

ThePiachu


People also ask

Does Go run tests concurrently?

Yes, tests are executed as goroutines and, thus, executed concurrently.

How long should test suite take to run?

The test suite you work with when you do TDD should execute in less than 10 seconds on your machine. If you have hundreds of tests, each test should be faster than 100 milliseconds. If you have thousands, each test should be faster than 10 milliseconds. You get the picture.

How do I test my go program?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.


1 Answers

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.

like image 155
Caleb Avatar answered Oct 19 '22 05:10

Caleb