Python 如何在烧瓶应用程序中使用unittest在测试用例中发送带有表单数据的图像?

Python 如何在烧瓶应用程序中使用unittest在测试用例中发送带有表单数据的图像?,python,flask,Python,Flask,我是Python新手,我正在制作Flask应用程序。因此,我想使用unittest为我的应用程序编写测试用例,我是这样做的: def test_bucket_name(self): self.test_app = app.test_client() response = self.test_app.post('/add_item', data={'name':'test_item','user_id':'1','username':'admin'})

我是Python新手,我正在制作Flask应用程序。因此,我想使用unittest为我的应用程序编写测试用例,我是这样做的:

def test_bucket_name(self):
    self.test_app = app.test_client()
    response = self.test_app.post('/add_item', data={'name':'test_item','user_id':'1','username':'admin'})                                      
    self.assertEquals(response.status, "200 OK")

这一切都很好。但是我在一个URL中发布了一些数据和图片。因此,我的问题是:“如何发送包含该数据的图像?

将图像读入
StringIO
缓冲区。将图像作为表单数据中的另一项传递,其中值是(图像,文件名)的元组


上面的答案是正确的,除了在我的情况下,我不得不使用BytesIO,如下所示:

    def create_business(self, name):
        with open('C:/Users/.../cart.jpg', 'rb') as img1:
            imgStringIO1 = BytesIO(img1.read())
        return self.app.post(
            '/central-dashboard',
            content_type='multipart/form-data',
            data=dict(name=name, logo=(imgStringIO1, 'cart.jpg')),
            follow_redirects=True
        )
    def create_business(self, name):
        with open('C:/Users/.../cart.jpg', 'rb') as img1:
            imgStringIO1 = BytesIO(img1.read())
        return self.app.post(
            '/central-dashboard',
            content_type='multipart/form-data',
            data=dict(name=name, logo=(imgStringIO1, 'cart.jpg')),
            follow_redirects=True
        )