如何为appengine go项目运行单元测试

时间:2013-06-18 12:17:42

标签: google-app-engine go

我在〜/ Applications / google_appengine

中有我的appengine SDK

在Eclipse中,我有一个外部工具设置来运行开发服务器。它指出:

〜/应用/ google_appengine / dev_appserver.py

同样在Eclipse(Go Preferences)中,我将GOROOT设置为指向

〜/应用/ google_appengine / GOROOT

现在,我正在尝试为我的项目运行一些单元测试。如果我使用我的常规安装(不是appengine安装),我会收到此错误:

../../context/context.go:4:2: cannot find package "appengine" in any of:
/usr/local/go/src/pkg/appengine (from $GOROOT)
/Users/home/src/go/foodbox/src/appengine (from $GOPATH)
../../context/data.go:4:2: cannot find package "appengine/datastore" in any of:
/usr/local/go/src/pkg/appengine/datastore (from $GOROOT)
/Users/home/src/go/foodbox/src/appengine/datastore (from $GOPATH)
../../context/context.go:5:2: cannot find package "appengine/user" in any of:
/usr/local/go/src/pkg/appengine/user (from $GOROOT)
/Users/home/src/go/foodbox/src/appengine/user (from $GOPATH)

如果我使用appengine go,我会得到这个:

load cmd/cgo: package cmd/cgo: no Go source files in /Users/home/Application/google_appengine/goroot/src/cmd/cgo

似乎默认安装无法找到appengine包(我猜这并不奇怪)。当我使用appengine go工具时,我不确定问题是什么。谁能告诉我如何让它发挥作用?

谢谢!

2 个答案:

答案 0 :(得分:5)

值得一提的是,appengine/aetest包现已包含在SDK since 1.8.6中。请参阅documentation

基本上,您可以在测试中使用appengine.Context,类似于icub3d/appenginetesting

引用文档中的示例:

import (
        "testing"

        "appengine/aetest"
)

func TestMyFunction(t *testing.T) {
        c, err := aetest.NewContext(nil)
        if err != nil {
                t.Fatal(err)
        }
        defer c.Close()

        // Run code and tests requiring the appengine.Context using c.
}

答案 1 :(得分:0)

参见文档:

http://blog.golang.org/appengine-dec2013

查看“本地单元测试”部分

我认为你要找的是sdk附带的goapp test