测试用例适用于go和appengine

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

Test cases for go and appengine

问题

我正在使用Go和appengine,现在我想做一些测试用例。

我尝试使用Go的标准测试包,
文件(都是"package hello"):

hello/http.go
hello/http_test.go

问题:我无法运行go test hello。我最接近的是go test hello/http_test.go,如果我不对http.go进行任何调用,它可以工作,但这样做没有意义。 测试用例适用于go和appengine

英文:

I am using Go and appengine, and now I would like to do some test cases.

I tried using gos standard test package,
Files (both "package hello"):

hello/http.go
hello/http_test.go

Problem: I cannot run go test hello. The closest I have got is go test hello/http_test.go which works if I do not make any calls to http.go, which is quite pointless. 测试用例适用于go和appengine

答案1

得分: 15

github.com/mzimmerman/appenginetesting

安装

  1. 安装Go

  2. 设置Go环境变量 (您的路径可能会有所不同):

     export GOPATH=~/gopath
     export PATH=$PATH:$GOPATH/bin
    
  3. 下载Google App Engine SDK for Go

  4. 设置Google App Engine环境变量 (您的路径可能会有所不同):

     export APPENGINE_SDK=$HOME/appengine
     export PATH=$PATH:$APPENGINE_SDK
    
  5. 创建appengineappengine_internal目录的符号链接:

     ln -s $APPENGINE_SDK/goroot/src/pkg/appengine $GOPATH/src/pkg/
     ln -s $APPENGINE_SDK/goroot/src/pkg/appengine_internal $GOPATH/src/pkg/
    
  6. 安装appenginetesting

     go get github.com/mzimmerman/appenginetesting
    

编写测试

appengintesting提供了一个虚拟的appengine.Context。在幕后,它启动一个Python开发服务器并通过它运行请求,因此测试可能会有点慢**(秒而不是毫秒)**。要在测试中使用它,您可以编写如下代码:

import "github.com/mzimmerman/appenginetesting"
...
c := appenginetesting.NewContext(nil)

然后,您可以像使用实际的appengine.Context一样使用c。这在测试文件中很有效,但对于通过调用appengine.NewContext(r)创建的上下文将无效。

我在gaego中使用的策略是从自定义包中导入上下文,而不是从appengine导入。这使我可以在构建为App Engine时使用appengine.Context,在构建为测试套件时使用appenginetesting.Context

通过设置以下构建标志:

  • context_appengine.go // +build appengine
  • context_testing.go // +build !appengine

编译器将决定加载哪个文件。示例。这种技术来自Gorilla

然后,我从我的包中导入,而不是从appengine导入,例如:

import (
  github.com/gaego/context
)

..
c := context.NewContext(r)
..

最后需要提到的是,您必须显式关闭上下文,否则Python进程将继续运行。您可以通过调用以下代码来终止进程:

defer c.Close()

有关更多示例,请查看:

context_test.go

recorder_test.go

编辑: Takuya Ueda创建了一个与最新SDK兼容的分支

编辑2: Joshua Marsh维护了一个与最新SDK兼容的分支

编辑3: Matt Zimmerman维护了一个具有标准aetest包之外的其他功能的分支(登录/注销和任务队列)

英文:

github.com/mzimmerman/appenginetesting

Installation

  1. Install Go

  2. Set the Go Environmental Variables (Your path may vary):

     export GOPATH=~/gopath
     export PATH=$PATH:$GOPATH/bin
    
  3. Download the Google App Engine SDK for Go

  4. Set the Google App Engine Environmental Variables (Your path may vary):

     export APPENGINE_SDK=$HOME/appengine
     export PATH=$PATH:$APPENGINE_SDK
    
  5. Symlink the appengine and appengine_internal dirctories:

     ln -s $APPENGINE_SDK/goroot/src/pkg/appengine $GOPATH/src/pkg/
     ln -s $APPENGINE_SDK/goroot/src/pkg/appengine_internal $GOPATH/src/pkg/
    
  6. Install appenginetesting

     go get github.com/mzimmerman/appenginetesting
    

Writing test

appengintesting provides a fake appengine.Context. Behind the scenes It's starts up a Python development server and runs the request through it, so tests can be a little bit slow (seconds instead of milliseconds). To use it in tests you write something like

import "github.com/mzimmerman/appenginetesting"
...
c := appenginetesting.NewContext(nil)

You can then use c as you would use an actual appengine.Context. This works will in the test file, but it won't work with contexts that you create by calling appengine.NewContext(r)

The strategy that I'm using in gaego is to import the context from a custom package instead of appengine. This allows me to use an appengine.Context when the build is for App Engine and use appenginetesting.Context when the build is for the test suit.

By setting the following build flags:

  • context_appengine.go // +build appengine
  • context_testing.go // +build !appengine

The compiler will decide which file it would like to load. Example. This techinique was taken form Gorilla

Then instead of import from appengine I import from my package E.g.

import (
  github.com/gaego/context
)

..
c := context.NewContext(r)
..

That last thing that needs to be mentioned is that you must explicitly close the context, otherwise the python processes will continue to run. You terminate the process by calling:

defer c.Close()

For more examples please view:

context_test.go

recorder_test.go

Edit: Takuya Ueda has created a brunch that works with the latest SDK

Edit2: Joshua Marsh maintains a fork that is compatible with the latest SDK

Edit3: Matt Zimmerman maintains a fork with additional features over the standard aetest package (Login/Logout & Task Queues)

答案2

得分: 2

一个有趣的进展:从1.8.6版本开始,通过"appengine/aetest"包将使用服务存根进行测试集成到SDK中。这与上述方法类似,通过一个"testing"上下文来实现。更多信息

英文:

An interesting development: as of 1.8.6 using service stubs for testing has been integrated into the SDK through the "appengine/aetest" package. This works largely like the above via a "testing" context. More info

huangapple
  • 本文由 发表于 2012年7月2日 07:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/11286534.html
匿名

发表评论

匿名网友

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

确定