本文介绍了测试功能与Google App Engine`文件`api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用Google Blobstore API的函数,这里有一个退化的例子:
$ b
#!/ usr / bin / python
from google.appengine.ext导入测试数据
def foo():
from google.appengine.api导入文件
blob_filename = files.blobstore.create (mime_type ='text / plain')
with files.open(blob_filename,'a')as googfile:
googfile.write(测试数据)
文件。 finalize(blob_filename)
tb = testbed.Testbed()
tb.activate()
tb.init_blobstore_stub()
foo()#in现实中,我是一个在unittest测试用例中从'虚拟客户'
#调用的函数。
产生的错误是:
Traceback(最近一次调用最后一次):
在
中的文件e.py,第18行foo()#实际上,我是一个函数,虚拟客户'
文件e.py,第8行,在foo
blob_filename = files.blobstore.create(mime_type ='text / plain')
文件/ Applications / GoogleAppEngineLauncher。 app / Contents / Resources / GoogleAppEngine-default.bundle / Contents / Resources / google_appengine / google / appengine / api / files / blobstore.py,第68行,创建
返回files._create(_BLOBSTORE_FILESYSTEM,params = params )
在_create $ b中的文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py,第491行$ b _make_call('Create',request,response)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_ appengine / google / appengine / api / files / file.py,第230行,在_make_call
rpc = _create_rpc(截止日期=截止日期)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine -default.bundle / Contents / Resources / google_appengine / google / appengine / api / files / file.py,第213行,在_create_rpc
返回apiproxy_stub_map.UserRPC('file',截止日期)
文件 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py,第393行,位于__init__
self .__ rpc = CreateRPC(service ,stubmap)
在CreateRPC $ b中的第67行中的/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py文件$ b assert stub,'No api proxy found for service%s'%service
AssertionError:No api proxy found for servicefile
我不想修改 foo
以便测试它。有没有办法让Google App Engine的单元测试中的 foo
按预期工作(即创建给定的文件)?
我希望能够通过,但我不明白这一点是否可以自行解决。
我很感激您的想法和建议。
感谢您的阅读。
看起来像 testbed.init_blobstore_stub()
已过期,因为 dev_appserver
不同于blobstore存根。这里是我的 init_blobstore_stub
的实现,它允许你在你的测试中写入和读取blobstore。
from google.appengine.ext从google.appengine.api.blobstore导入测试床
从google.appengine.api.files导入blobstore_stub,file_blob_storage
import file_service_stub
$ b $ class TestbedWithFiles(testbed.Testbed):
$ b $ 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)
#您的代码...
def foo():
from google.appengine.api导入文件s
blob_filename = files.blobstore.create(mime_type ='text / plain')
with files.open(blob_filename,'a')as googfile:
googfile.write(测试数据)
files.finalize(blob_filename)
tb = TestbedWithFiles()
tb.activate()
tb.init_blobstore_stub()
foo()
I have a function that uses the Google Blobstore API, and here's a degenerate case:
#!/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.
The error this generates is:
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"
I don't want to have to modify foo
in order to be able to test it. Is there a way to make foo
work as expected (i.e. create the given file) in Google App Engine's unit tests?
I would expect to be able to do this with Google's API Proxy, but I don't understand it well enough to figure it out on my own.
I'd be grateful for your thoughts and suggestions.
Thanks for reading.
解决方案
It seems like testbed.init_blobstore_stub()
is outdated, because dev_appserver
inits blobstore stubs differently. Here is my implementation of init_blobstore_stub
that allows you to write to and read from blobstore in your tests.
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()
这篇关于测试功能与Google App Engine`文件`api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!