Testing file upload with Flask and Python 3 Testing file upload with Flask and Python 3 flask flask

Testing file upload with Flask and Python 3


In Python 3, you need to use io.BytesIO() (with a bytes value) to simulate an uploaded file:

rv = self.app.post('/add', data=dict(                               file=(io.BytesIO(b"this is a test"), 'test.pdf'),                           ), follow_redirects=True)

Note the b'...' string defining a bytes literal.

In the Python 2 test examples, the StringIO() object holds a byte string, not a unicode value, and in Python 3, io.BytesIO() is the equivalent.