问题描述
我喜欢编写一些单元测试,除其他外,还需要
如何编写一个单元测试 setUp
blobstore,以便它可以通过以下方式阅读:
pre $ blob_info = blob_info = blob_key
reader = BlobReader(blob_info)
reader.readline()
编辑:
不寻找测试的方法,我想在测试用例blobstore存储中放置一些任意数据,以避免测试用例setUp阶段,所以我可以针对这些数据运行测试。
您可以将以下内容添加到您的setUp方法中,并可能存储 blob_key
作为 self.blob_key
供以后使用。 init_files_stub非常重要,因为它使用内存blobstore初始化文件服务。
self.testbed.init_blobstore_stub()
self.testbed.init_files_stub()
from google.appengine.api导入文件
file_name = files.blobstore.create(mime_type ='application / octet-stream')
with files.open( file_name,'a')as f:
f.write('blobdata')
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
请注意,testbed引用来自google.appengine.ext的 import testbed
和 self.testbed
是测试平台实例。
使用init_files_stub,这与:
Id like to write some unit tests that among other thing will need to read a blobstore file
How to write a unit test setUp
that puts some file in testbed blobstore so it will availabe for read this way:
blob_info = BlobInfo(blob_key)
reader = BlobReader(blob_info)
reader.readline()
EDIT:
I do not look for a way to test files API, I want to put some arbitrary data in the testbed blobstore storage dusring the test case setUp phase, so I can run tests against this data.
You can add the following to your setUp method, and perhaps store the blob_key
as self.blob_key
for later use. The init_files_stub is important as it initializes the file service with the memory blobstore.
self.testbed.init_blobstore_stub()
self.testbed.init_files_stub()
from google.appengine.api import files
file_name = files.blobstore.create(mime_type='application/octet-stream')
with files.open(file_name, 'a') as f:
f.write('blobdata')
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
Note that testbed refers to from google.appengine.ext import testbed
and self.testbed
is the testbed instance.
With init_files_stub, this is exactly as described in docs:
这篇关于如何使用GAE dev服务器testbed for python模拟文件上传到blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!