How to create a Flask object for testing

I am trying to write a test for a function that uses a file that has been uploaded through http and that read using flask, for example:

content = flask.request.files["myfile"].read()

I am not totally clear how this works,I understand that flask would output FileStorage objects but I am not clear what type of object read() would output. In any case, for a test I would like to create content from my own file of the same type as flask.request.files[“file”].read() so that I can test downstream functions, how can I do that? For example:

testContent = pythonFunction("testFile.csv")

which would be of the same type as flask.request.files[“myfile”].read()

Hope this makes sense. Any suggestions appreciated.

flask.request.files["myfile"] is a werkzeug.datastructures.FileStorage instance, which has a stream attribute. stream must be an io instance of some kind, which implements a read method. It returns either str or bytes, depending on the type of io object.

To write tests for this that can run independently of your production environment, you will need to patch either flask or werkzeug to return appropriate data when run inside the test environment. How to do that depends on the testing framework you are using. The unittest library has mock, pytest has monkeypatch as well as the pytest-mock plugin. I’m sure nose has something similar.