how to simulate image upload to google app engine

2019-04-14 20:38发布

I'm uploading images to the GAE blobstore using create_upload_url

uploadURL = blobstore.create_upload_url('/upload')

For the purpose of unit testing the gae code, can you simulate the image upload? OR should I insert the image data in my test bed and assume the upload is successful? If so, how do you upload an image to the test bed?

2条回答
Explosion°爆炸
2楼-- · 2019-04-14 21:03

Agree with @fredrik on what exactly you're testing there.

Anyway, if you're doing some functional/blackbox/similar testing, you could simply use Webtest framework (see post method) and do the actual upload, e.g.

payload = [(fieldname, filename)]
test_app.post(uploadURL, upload_files=payload)

Have a look at Handler Testing for Python for details on how to initialize the above test_app.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-14 21:09

Could you provided some code on how your test look?

I think you should be able to fake a request to the upload_url using webapp2. Have a look here for some sample code on how to fake requests.

On the other hand you should think of what the purpose of your test is. Is the purpose to test that the image upload works or is it how your code works after the upload is complete?

When running unit-tests try to break the dependencies to other libraries so that you only test you own code. And then add a new suite of implementation test, ie make a request for and url and check that you get the expected response. As in test_redirect_if_no_session of the example above, make a request to a page that requires a user and expect a redirect (http response code 302).

..fredrik

查看更多
登录 后发表回答