Django如何测试httprespons中返回的图像

2024-05-14 15:04:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个django测试方法,它检查作为httpresponse返回的图像是否等于在将http请求发送到视图之前打开的图像:

def test_uploaded_file(self):
    c = Client()
    originalFilePath = '../static_cdn/test_img.jpg'
    image_data = open(originalFilePath, "rb")
    with open(originalFilePath, "rb") as fp:
        response = c.post('/', {'image': fp})
        self.assertEqual(image_data,response)

由于某些原因,测试返回以下错误:

^{pr2}$

我认为这个问题与以下事实有关:从视图返回的图像是httpresponse,而在测试方法中打开的另一个图像则不是。为什么这个测试会失败?能否从响应中提取图像?


Tags: djangotest图像imageself视图httpdata

热门问题