GAE:使用 testbed 和 webtest 测试 blob 的下载

发布于 2025-01-06 00:21:32 字数 1973 浏览 3 评论 0原文

我将 blobstore 与我的 Google App Engine 应用程序一起使用,并且在生产服务器和开发服务器上一切正常。然而,使用 testbed 和 webtest 进行测试不起作用......

在我的测试中,该斑点存在于我可以这样访问它:

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

当我尝试在这样的测试中下载 blob 时,

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

我的 blobstore 下载处理程序永远不会被调用,并且收到 404 错误(但该链接在开发或生产服务器上有效)。

我怀疑这是 testbed 或 webtest 的错误...

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


以下是有关我如何设置测试的一些信息。

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

这是 app.yaml 的相关部分:

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

这是 main.py 的相关部分:

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

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),
    ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2025-01-13 00:21:32

如果没有来自 main.application 和 app.yaml 的路由,就很难讲述路由。

我怀疑您在 app.yaml 中配置了“/blob-download”,而 webtest 不知道它,它只知道您在 main.application 中配置的路由。

更新:现在 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 在这里不会为您提供帮助(但它不会解释 404)。

您可以做的是运行完整的端到端测试,例如使用 gaedriver:http:// code.google.com/p/gaedriver/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文