django.test.Client和response.content vs. streaming_content

zmb*_*mbq 5 python testing django django-1.6

我有一个使用Django测试客户端访问网页的Django测试.

在其中一个测试中,服务器返回ZIP文件作为附件.我使用以下代码访问ZIP文件的内容:

zip_content = StringIO(response.content)
zip = ZipFile(zip_content)
Run Code Online (Sandbox Code Playgroud)

这会导致以下弃用警告:

D:/ Developments/Archaeology/DB/ArtefactDatabase/Webserver\importexport\tests\test_import.py:1:DeprecationWarning:不推荐访问content流式响应中的属性.请改用streaming_content属性

response.streaming_content返回某种地图,这绝对不是一个类似文件的对象ZipFile.我该如何使用该streaming_content属性?

顺便说一句,我只是在传递response.content给a StringIO时得到了弃用警告,当我访问普通HTML页面的response.content时,没有警告.

Ris*_*nha 6

使用Python 3.4.

带字符串:

zip_content = io.StringIO("".join(response.streaming_content))
zip = ZipFile(zip_content)
Run Code Online (Sandbox Code Playgroud)

与字节:

zip_content = io.BytesIO(b"".join(response.streaming_content))
zip = ZipFile(zip_content)
Run Code Online (Sandbox Code Playgroud)

https://github.com/sio2project/oioioi/blob/master/oioioi/filetracker/tests.py的 TestStreamingMixin中找到的解决方案

另见:https: //docs.djangoproject.com/en/1.7/ref/request-response/

您可能希望通过检查response.streaming(布尔值)来测试响应是否是流.


Mau*_*ldi -3

您应该改变测试方法。它response.streaming_content完全按照它的目的去做。刚刚测试调用下载是可以的。

如果要测试文件生成/完整性方法,则需要单独测试其功能。无论您的文件是 Django Test 的 ZIP 还是 CSV,都没关系,但如果您的调用没问题的话。

  • 集成测试的目的是测试调用及其结果的正确性。如果不检查响应的内容是否正确,就不足以说测试通过。 (7认同)