如何运行AppEngine Go项目的单元测试

huangapple go评论74阅读模式
英文:

How to run unit tests for appengine go project

问题

我在~/Applications/google_appengine中有我的appengine SDK。

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

~/Application/google_appengine/dev_appserver.py

同样在Eclipse中(进入首选项),我设置了GOROOT指向

~/Application/google_appengine/goroot

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

../../context/context.go:4:2: 无法在以下任何位置找到包“appengine”:
/usr/local/go/src/pkg/appengine(来自$GOROOT)
/Users/home/src/go/foodbox/src/appengine(来自$GOPATH)
../../context/data.go:4:2: 无法在以下任何位置找到包“appengine/datastore”:
/usr/local/go/src/pkg/appengine/datastore(来自$GOROOT)
/Users/home/src/go/foodbox/src/appengine/datastore(来自$GOPATH)
../../context/context.go:5:2: 无法在以下任何位置找到包“appengine/user”:
/usr/local/go/src/pkg/appengine/user(来自$GOROOT)
/Users/home/src/go/foodbox/src/appengine/user(来自$GOPATH)

如果我使用appengine的go,我会得到以下错误:

load cmd/cgo: package cmd/cgo: 在/Users/home/Application/google_appengine/goroot/src/cmd/cgo中没有Go源文件

似乎默认安装无法找到appengine包(我猜这并不奇怪)。当我使用appengine的go工具时,我不确定问题是什么。有人可以告诉我如何解决这个问题吗?

谢谢!

英文:

I have my appengine SDK in ~/Applications/google_appengine

In Eclipse, i've got an external tool setup to run the dev server. It's pointed at:

~/Application/google_appengine/dev_appserver.py

Also in Eclipse (Go Preferences), I've set the GOROOT to point to

~/Application/google_appengine/goroot

Now, I'm trying to run some unit tests for my project. If I use my regular go installation (not the appengine one), I get this error:

../../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)

If i use the appengine go, I get this one:

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

It seems like the default installation can't find the appengine packages (I guess that's not surprising). I'm not sure what the problem is when I use the appengine go tools. Can anyone tell me how to get this to work?

Thanks!

答案1

得分: 5

可能值得一提的是,appengine/aetest包现在已经包含在SDK中,自1.8.6版本起。请参阅文档

基本上,您可以在测试中使用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()

        // 使用c运行需要appengine.Context的代码和测试。
}
英文:

It may be worth mentioning that the appengine/aetest package is now included in the SDK, since 1.8.6. See the documentation.

Basically you get an appengine.Context that can be used in your tests, similar to icub3d/appenginetesting.

Quoting the example from the docs:

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

得分: 0

查看文档:

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

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

我认为你要找的是随 SDK 一起提供的 goapp test

英文:

See the docs:

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

Check out the section called "Local Unit Testing"

I think what you are looking for is goapp test that comes with the sdk.

huangapple
  • 本文由 发表于 2013年6月18日 20:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/17168653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定