GAE:使用测试平台和webtest测试blob的下载 [英] GAE: testing download of blobs with testbed and webtest

查看:104
本文介绍了GAE:使用测试平台和webtest测试blob的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的Google App Engine应用程序使用blobstore,并且生产服务器和开发服务器上的所有内容都正常工作。然而,使用测试平台和 webtest 进行测试无法正常运行......



在我的测试中,blob存在,因为我可以像这样访问它:

  blob = self.blobstore_stub .storage._blobs [key] 

当我尝试在我的测试中下载blob时, p>

  response = self.app.get(/ blob-download / 2)

我的blobstore下载处理程序永远不会被调用,并且我得到一个404错误(但链接在dev或prod服务器上运行)。



我怀疑这是测试床或webtest的错误...



任何关于我可能会做错的想法,或者如果这是testbed / webtest的错误什么是一个很好的解决办法可以让我可以测试这部分代码?






<

  import unittest $ b $ from webtest import Tes tApp 
from google.appengine.ext从google.appengine.api导入db,testbed
从google.appengine.api导入用户
import apiproxy_stub_map

class ExampleTests( unittest.TestCase):

def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.setup_env(app_id =stv)
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_taskqueue_stub()
self.testbed.init_mail_stub()
self.testbed。 init_blobstore_stub()
self.app = TestApp(main.application)
apiproxy_stub_map.apiproxy.GetStub(datastore_v3)。Clear()
self.taskqueue_stub = apiproxy_stub_map.apiproxy.GetStub(' taskqueue')
self.mail_stub = apiproxy_stub_map.apiproxy.GetStub('mail')
self.blobstore_stub = apiproxy_stub_map.apiproxy.GetStub('blobstore')

def testBlob( self):
#使用创建blob files.blobstore.create
response = self.app.get(/ blob-download / 2)#这将返回404
self.assertEqual(response.body,blob的内容)#This失败






这是app.yaml的相关部分:

 处理程序:
- url:/.*
脚本:main.application






这是main.py的相关部分:

  application = webapp2.WSGIApplication(
[
('/ blob-download /([^ /] +)? ',views.BlobDownload),
]


解决方案

如果没有main.application和app.yaml的路由可用,很难讲述路由。

我怀疑你在app.yaml中配置了/ blob-download,其中webtest不知道,它只知道你在main.application中配置的路由。 / p>

更新:现在我们现在app.yaml不是原因,我们继续前进。有帮助的是看到你的处理程序。 Blobstore服务响应的处理方式与通常的响应不同。作为开发人员,您将blob密钥作为头添加到响应中。 App Engine后端检查这个头部结束,并且如果它发现它接管了blob的服务。您可以在这里查看dev_appserver实现:
http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/dev_appserver_blobstore.py#214 。这意味着你不能在没有dev_appserver或appserver处理请求的情况下真正测试blob的服务 - 这意味着testbed + webtest不会帮助你在这里(它不会帮助你)你可以做的是运行一个完整的端到端测试,例如使用gaedriver:
/code.google.com/p/gaedriver/rel =nofollow> http://code.google.com/p/gaedriver/


I'm using the blobstore with my Google App Engine app, and everything is working fine on the production server and the development server. Testing with testbed and webtest, however, isn't working...

In my tests, the blob exists as I can access it like this:

blob = self.blobstore_stub.storage._blobs[key]

When I try to download a blob in my tests like this

response = self.app.get("/blob-download/2")

my blobstore download handler never gets called and I get a 404 error (but the link works on the dev or prod servers).

I suspect this is an error with testbed or webtest...

Any ideas as to what I might be doing wrong, or if this is an error with testbed/webtest what a good work around might be so that I can test this part of my code?


Here is some info about how I am setting up my tests.

import unittest
from webtest import TestApp
from google.appengine.ext import db, testbed
from google.appengine.api import users
from google.appengine.api import apiproxy_stub_map

class ExampleTests(unittest.TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.setup_env(app_id="stv")
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_taskqueue_stub()
        self.testbed.init_mail_stub()
        self.testbed.init_blobstore_stub()
        self.app = TestApp(main.application)
        apiproxy_stub_map.apiproxy.GetStub("datastore_v3").Clear()
        self.taskqueue_stub = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
        self.mail_stub = apiproxy_stub_map.apiproxy.GetStub('mail')
        self.blobstore_stub = apiproxy_stub_map.apiproxy.GetStub('blobstore')

   def testBlob(self):
        # create blob using files.blobstore.create
        response = self.app.get("/blob-download/2") # This returns 404
        self.assertEqual(response.body, "content of blob") # This fails


This is the relevant portion of app.yaml:

handlers:
- url: /.*
  script: main.application


This is the relevant portion of main.py:

application = webapp2.WSGIApplication(
    [
     ('/blob-download/([^/]+)?', views.BlobDownload),
    ]

解决方案

It's hard to tell about the routing without having the routing from main.application and app.yaml available.

I suspect that you configured "/blob-download" in app.yaml of which webtest is unaware, it only knows about the routing you configured in main.application.

update: Now that we now app.yaml is not the cause, let's move on. What would help is to see your handler. Blobstore serving responses are handled differently then the usual responses. You, as a developer, add the blob key as a header to the response. The App Engine backend checks for this header end and if it finds it takes over the serving of the blob. You can check out the dev_appserver implementation here: http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/dev_appserver_blobstore.py#214.

This means you can't actually test serving of blobs without having dev_appserver or appserver processing the requests - which means testbed + webtest won't help you here (it doesn't explain the 404 though).

What you could do is run a full end-to-end test, for example with gaedriver: http://code.google.com/p/gaedriver/

这篇关于GAE:使用测试平台和webtest测试blob的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆