django测试文件下载 - “ValueError:关闭文件上的I / O操作” [英] django test file download - "ValueError: I/O operation on closed file"
问题描述
我有一个视图的代码,它提供文件下载,在浏览器中可以正常工作。现在我正在尝试使用内部的django Client.get来编写一个测试:
I have code for a view which serves a file download, and it works fine in the browser. Now I am trying to write a test for it, using the internal django Client.get:
response = self.client.get("/compile-book/", {'id': book.id})
self.assertEqual(response.status_code, 200)
self.assertEquals(response.get('Content-Disposition'),
"attachment; filename=book.zip")
现在我想测试下载的文件是否是我希望下载的文件。所以我开始说:
so far so good. Now I would like to test if the downloaded file is the one I expect it to download. So I start by saying:
f = cStringIO.StringIO(response.content)
现在我的测试运行员回应:
Now my test runner responds with:
Traceback (most recent call last):
File ".../tests.py", line 154, in test_download
f = cStringIO.StringIO(response.content)
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 282, in content
self._consume_content()
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in _consume_content
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in <genexpr>
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/usr/lib/python2.7/wsgiref/util.py", line 30, in next
data = self.filelike.read(self.blksize)
ValueError: I/O operation on closed file
即使我简单地说:self.assertIsNotNone(response.content)我得到相同的ValueError
Even when I do simply: self.assertIsNotNone(response.content) I get the same ValueError
只有在整个互联网上的主题(包括django文档)我可以找到有关测试下载的内容是这个stackoverflow主题:用于测试文件下载的Django Unit Test 。尝试该解决方案导致了这些结果。对于我来说,开放一个新问题是老旧且很少的。
The only topic on the entire internet (including django docs) I could find about testing downloads was this stackoverflow topic: Django Unit Test for testing a file download. Trying that solution led to these results. It is old and rare enough for me to open a new question.
有人知道如何在Django中处理下载测试? (btw,在python 2.7上运行django 1.5)
Anybody knows how the testing of downloads is supposed to be handled in Django? (btw, running django 1.5 on python 2.7)
推荐答案
这对我们有用。我们返回 rest_framework.response.Response
,但它也适用于常规的Django响应。
This works for us. We return rest_framework.response.Response
but it should work with regular Django responses, as well.
import io
response = self.client.get(download_url, {'id': archive_id})
downloaded_file = io.BytesIO(b"".join(response.streaming_content))
注意:
streaming_content
仅适用于 StreamingHttpResponse
(还有Django 1.10):
https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.StreamingHttpResponse.streaming_content
Note:
streaming_content
is only available for StreamingHttpResponse
(also Django 1.10):
https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.StreamingHttpResponse.streaming_content
这篇关于django测试文件下载 - “ValueError:关闭文件上的I / O操作”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!