Testing file uploads in Flask

前端 未结 4 417
庸人自扰
庸人自扰 2021-02-02 07:32

I\'m using Flask-Testing for my Flask integration tests. I\'ve got a form that has a file upload for a logo that I\'m trying to write tests for but I keep getting an error sayin

4条回答
  •  心在旅途
    2021-02-02 08:04

    You need two things:

    1.) content_type='multipart/form-data' in your .post()
    2.) in your data= pass in file=(BytesIO(b'my file contents'), "file_name.jpg")

    A full example:

        data = dict(
            file=(BytesIO(b'my file contents'), "work_order.123"),
        )
    
        response = app.post(url_for('items.save'), content_type='multipart/form-data', data=data)
    

提交回复
热议问题