测试功能与Google App Engine`文件`api

我有一个使用Google Blobstore API的函数,下面是一个退化的例子:

#!/usr/bin/python
from google.appengine.ext import testbed

def foo():
    from google.appengine.api import files
    blob_filename = files.blobstore.create(mime_type='text/plain')
    with files.open(blob_filename, 'a') as googfile:
        googfile.write("Test data")

    files.finalize(blob_filename)

tb = testbed.Testbed()
tb.activate()
tb.init_blobstore_stub()

foo() # in reality, I'm a function called from a 'faux client'
      # in a unittest testcase.

产生的错误是:

Traceback (most recent call last):
  File "e.py", line 18, in 
    foo() # in reality, I'm a function called from a 'faux client'
  File "e.py", line 8, in foo
    blob_filename = files.blobstore.create(mime_type='text/plain')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/blobstore.py", line 68, in create
    return files._create(_BLOBSTORE_FILESYSTEM, params=params)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 491, in _create
    _make_call('Create', request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 230, in _make_call
    rpc = _create_rpc(deadline=deadline)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 213, in _create_rpc
    return apiproxy_stub_map.UserRPC('file', deadline)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 393, in __init__
    self.__rpc = CreateRPC(service, stubmap)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 67, in CreateRPC
    assert stub, 'No api proxy found for service "%s"' % service
AssertionError: No api proxy found for service "file"

我不想修改foo以便能够测试它。 在Google App Engine的单元测试中,有没有办法让foo按预期工作(即创建给定的文件)?

我希望能够通过Google的API代理实现这一点,但我不能很好地理解我自己的想法。

我会很感激你的想法和建议。

谢谢阅读。


看起来好像testbed.init_blobstore_stub()已经过时了,因为dev_appserver不同的方式存在blobstore存根。 这里是我的init_blobstore_stub实现,它允许你在测试中写入和读取blobstore。

from google.appengine.ext import testbed
from google.appengine.api.blobstore import blobstore_stub, file_blob_storage
from google.appengine.api.files import file_service_stub

class TestbedWithFiles(testbed.Testbed):

    def init_blobstore_stub(self):
        blob_storage = file_blob_storage.FileBlobStorage('/tmp/testbed.blobstore',
                                                testbed.DEFAULT_APP_ID)
        blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
        file_stub = file_service_stub.FileServiceStub(blob_storage)
        self._register_stub('blobstore', blob_stub)
        self._register_stub('file', file_stub)

# Your code...
def foo():
    from google.appengine.api import files
    blob_filename = files.blobstore.create(mime_type='text/plain')
    with files.open(blob_filename, 'a') as googfile:
        googfile.write("Test data")

    files.finalize(blob_filename)

tb = TestbedWithFiles()
tb.activate()
tb.init_blobstore_stub()

foo()

任何尝试使用gaeunit.py测试运行器执行此操作的机会? 我在使用它时看到了相同的错误,因为它是自己的代码来替换api代理。

当我将'file'添加到gaeunit.py的_run_test_suite函数中的“原样”代理列表中时,该错误消失。

老实说,我不确定是否需要gaeunit.py代理替换代码,因为我还使用了http://code.google.com/appengine/docs中测试用例中最近推荐的测试代码/python/tools/localunittesting.html。 所以,在这一点上,我已经评论了所有的gaeunit.py,这似乎也在工作。

请注意,我只在开发服务器上完成所有这些工作,在python27的高度实验模式下使用Python 2.7进行GAE。

希望这可以帮助。


我不知道它是否稍后添加到SDK中,但使用Testbed.init_files_stub应该修复它:

tb = testbed.Testbed()
tb.activate()
tb.init_blobstore_stub()
tb.init_files_stub()
链接地址: http://www.djcxy.com/p/88691.html

上一篇: test function with Google App Engine `files` api

下一篇: Can't load jinja2 with webapp2/Google App Engine