【发布时间】:2016-06-11 14:23:51
【问题描述】:
我正在使用Flask-Testing 进行我的 Flask 集成测试。我有一个表单,其中包含我正在尝试为其编写测试的徽标的文件上传,但我一直收到错误消息:TypeError: 'str' does not support the buffer interface。
我正在使用 Python 3。我找到的最接近的答案是 this,但它不适合我。
这是我的许多尝试之一:
def test_edit_logo(self):
"""Test can upload logo."""
data = {'name': 'this is a name', 'age': 12}
data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg')
self.login()
response = self.client.post(
url_for('items.save'), data=data, follow_redirects=True)
})
self.assertIn(b'Your item has been saved.', response.data)
advert = Advert.query.get(1)
self.assertIsNotNone(item.logo)
如何在 Flask 中测试文件上传?
【问题讨论】:
-
如果有一个可复制粘贴的可重现示例和完整的回溯,这个问题会更好。事实上,它并没有告诉我们是哪一行引发了引用的错误,也没有让我们自己重现它变得微不足道,并且对问题的第一次编辑使mmcclannahan's answer 部分荒谬。有点不必要的混乱!