当使用allure.attach.file附加文件时,可能会遇到'Object of type AttachmentType is not JSON serializable”错误。这是因为某些文件类型不能直接序列化为JSON格式。 解决此问题的方法是通过将文件转换为字节数组并将其与文件名一起附加到allure附件中来避免使用附件类型对象。以下是示例代码:
import allure
import requests
def test_attach_file():
response = requests.get('https://example.com/image.jpg')
with allure.step('Attach file'):
allure.attach(response.content, name='image.jpg', attachment_type=allure.attachment_type.JPG)
通过使用response.content和文件名来附加文件,我们可以避免使用AttachmentType对象并解决此错误。