问题
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.
回答1:
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:
来源:https://stackoverflow.com/questions/10870968/how-to-simulate-file-upload-to-blobstore-using-gae-dev-server-testbed-for-python