【发布时间】:2018-03-15 01:21:50
【问题描述】:
我有一个 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)
由于某种原因,测试返回此错误:
AssertionError: <open file u'../static_cdn/test_img.jpg', mode 'rb' at 0x7fed326b6a50> != <HttpResponse status_code=200, "image/jpg">
我认为问题与从视图返回的图像是 httpresponse 的事实有关,而在测试方法中打开的另一个图像不是。 为什么这个测试会失败?可以从响应中提取图像吗?
【问题讨论】:
标签: python django unit-testing http django-views