【问题标题】:How to run unit tests for appengine go project如何为 appengine go 项目运行单元测试
【发布时间】:2013-06-18 12:17:42
【问题描述】:

我的 appengine SDK 在 ~/Applications/google_appengine 中

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

~/Application/google_appengine/dev_appserver.py

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

~/Application/google_appengine/goroot

现在,我正在尝试为我的项目运行一些单元测试。如果我使用常规的 go 安装(不是 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 工具时,我不确定问题出在哪里。谁能告诉我如何让它工作?

谢谢!

【问题讨论】:

标签: google-app-engine go


【解决方案1】:

值得一提的是,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.
}

【讨论】:

    【解决方案2】:

    查看文档:

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

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

    我认为您正在寻找的是 sdk 附带的goapp test

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2015-04-12
      • 1970-01-01
      • 2021-07-31
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多