doudao7113 2013-10-16 15:21
浏览 47
已采纳

如何对Google App Engine Go HTTP处理程序进行单元测试?

Local unit testing is supported from version 1.8.6 of the Google App Engine Go SDK. The appengine/aetest package allows me to create a Context to unit test with.

How can I use this with net/http/httptest to test my HTTP handlers?

  • 写回答

2条回答 默认 最新

  • dongrunying7537 2013-10-16 17:58
    关注

    See the top of goroot/src/pkg/appengine/aetest/context.go (updated source is not yet posted at https://code.google.com/p/appengine-go). At first glance, the new testing app looks to be a slightly beefier/different version of appenginetesting so you can do the same sorts of tests, see here for one way to do it with how sampleHandler(w http.ResponseWriter, r *http.Request) is called.

    Alternatively, you can make your http.Handler's ContextHandler like as below:

    type ContextHandler struct {
        Real func(*appengine.Context, http.ResponseWriter, *http.Request)
    }
    
    func (f ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        c := appengine.NewContext(r)
        f.Real(c, w, r)
    }
    
    func myNewHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
    // do something
    }
    

    Then you can do this in init() to support production:

    http.Handle("/myNewHandler", ContextHandler{myNewHandler})
    

    This makes testing the function easy:

    func TestMyNewHandler(t *testing.T) {
        c := aetest.NewContext()
        r, _ := http.NewRequest("GET", "/tasks/findOverdueSchedules", nil)
        w := httptest.NewRecorder()
        myNewHandler(c, w, r)
        if 200 != w.Code {
            t.Fail()
        }
    }
    

    Here's what's from context.go inside appengine/aetest:

    /* Package aetest provides an appengine.Context for use in tests.

    An example test file: package foo_test

    import (
        "testing"
    
        "appengine/memcache"
        "appengine/aetest"
    )
    
    func TestFoo(t *testing.T) {
        c, err := aetest.NewContext(nil)
        if err != nil {
            t.Fatal(err)
        }
        defer c.Close()
    
        it := &memcache.Item{
            Key:   "some-key",
            Value: []byte("some-value"),
        }
        err = memcache.Set(c, it)
        if err != nil {
            t.Fatalf("Set err: %v", err)
        }
        it, err = memcache.Get(c, "some-key")
        if err != nil {
            t.Fatalf("Get err: %v; want no error", err)
        }
        if g, w := string(it.Value), "some-value" ; g != w {
            t.Errorf("retrieved Item.Value = %q, want %q", g, w)
        }
    }
    

    The environment variable APPENGINE_API_SERVER specifies the location of the api_server.py executable to use. If unset, the system PATH is consulted. */

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000