我在Flask集成测试中使用Flask-Testing。我有一个表单,该表单具有我要为其编写测试的徽标的文件上传,但是我不断收到错误消息:TypeError: 'str' does not support the buffer interface
。
我正在使用Python3。我找到的最接近的答案是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中测试文件上传?
python大神给出的解决方案
问题最终不是当人们将content_type='multipart/form-data'
添加到post
方法时,它期望data
中的所有值都是文件或字符串。我的数据字典中有整数,这要归功于this注释。
因此,最终解决方案最终看起来像这样:
def test_edit_logo(self):
"""Test can upload logo."""
data = {'name': 'this is a name', 'age': 12}
data = {key: str(value) for key, value in data.items()}
data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg')
self.login()
response = self.client.post(
url_for('adverts.save'), data=data, follow_redirects=True,
content_type='multipart/form-data'
)
self.assertIn(b'Your item has been saved.', response.data)
advert = Item.query.get(1)
self.assertIsNotNone(item.logo)
USB设备发行 - python我目前正在使用PyUSB。由于我不熟悉USB,所以我不知道如何执行以下操作。我已经从Python PyUSB成功连接到我的USB设备硬件。在代码中,我需要重置USB设备硬件。通过向硬件发送命令来完成。现在,在硬件重置后,我想从Python PyUSB释放当前的USB设备。然后,我想在重置后将其重新连接到USB设备硬件。请让我知道,如何释放USB设备连接和接口…
如果__name__ =='__main__',则为Python的Powershell等效项: - python我真的很喜欢python的功能,例如:if __name__ == '__main__': #setup testing code here #or setup a call a function with parameters and human format the output #etc... 很好,因为我可以将Python脚本文件…
PyCharm中Django的文档字符串中未解决的引用 - python我在Django的专案中使用Google Style Python Docstrings like in this Example。当我创建一个类并在文档字符串中使用属性符号时,Pycharm总是说-“未解决的引用”。class Post(models.Model): """ Class for posts. Attribute…
Sklearn将字符串类标签更改为int - python我有一个pandas数据框,我试图将给定列中的值更改为字符串表示的整数。例如:df = index fruit quantity price 0 apple 5 0.99 1 apple 2 0.99 2 orange 4 0.89 4 banana 1 1.64 ... 10023 kiwi 10 0.92 我想看一下:df = index fruit q…
带有参数FlasK / Rest API的GET请求 - python我正在尝试执行GET请求,该请求应根据设置的参数从数据库中打印特定行。参数应该是一门课程的名称,我希望它从所选课程中获取所有数据。将其解释为SQL查询可能会更容易一些。该查询可能看起来像这样“ SELECT * FROM courselist WHERE course ='D0024E';”那里的“课程”。我已经设法完成fetchall()并从特定表中接收了…