Created
September 23, 2012 17:48
-
-
Save lost-theory/3772472 to your computer and use it in GitHub Desktop.
upload file with flask test client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from app import app | |
from cStringIO import StringIO | |
class UploadTest(unittest.TestCase): | |
def setUp(self): | |
self.app = app | |
self.app.config['TESTING'] = True | |
self.client = self.app.test_client() | |
def test_upload(self): | |
res = self.client.post('/', data=dict( | |
upload_var=(StringIO("hi everyone"), 'test.txt'), | |
)) | |
assert res.status_code == 200 | |
assert 'file saved' in res.data | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@PVinay737 you need to turn tracebacks on so you can see what's causing the 400 error. Here is what I've used in the past to do that:
https://github.com/lost-theory/moviepicker/blob/be36832/tests/__init__.py#L24
There's also a note on this in the flask docs on testing:
http://flask.pocoo.org/docs/0.12/testing/
Once you see the underlying traceback (instead of just the 400 error code) you will be able to see where the bug is (either in the way you're making the request in the test code, or if there's a problem in the view code, or something else).