mercredi 25 novembre 2015

go/appengine: faster datastore tests

Recently I've been playing around with golang and google's app engine.

I've been experiencing really slow unit test executions when implementing repositories.

The thing I do is to call ctx,_ := aetest.NewContext(nil) in each test in order to obtain a clean database. This starts a new server in each test and thus leads in slow testing.

Lately I've been trying to work this around by starting it in TestMain.

var ctx aetest.Context

func TestMain(m *testing.M) {
    ctx,_ = aetest.NewContext(nil)
    code := m.Run()
    ctx.Close()
    os.Exit(code)
}

func TestMyRepository(t *testing.T){
    cleanDatastore()
    repo := &MyRepository{ctx}
    repo.DoSomething()
}

In the function cleanDatastore I've been executing a bash script that basically runs a SQLite command to clean the local database saved in /tmp.

#!/usr/bin/env bash

PATH=$1
cd $PATH

echo "Cleaning datastore..."

/usr/bin/sqlite3 datastore "delete from \"dev~testapp!!EntitiesByProperty\";"
/usr/bin/sqlite3 datastore "delete from \"dev~testapp!!Entities\";"

echo "Datastore is clean."

Is what I am trying to do making any sense or is there a simpler way to achieve better testing time.

Aucun commentaire:

Enregistrer un commentaire