django.test.Client and response.content vs. streaming_conten



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

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

zip_content = StringIO(response.content)
zip = ZipFile(zip_content)

这会导致以下弃用警告:

D:/Developments/Archaeology/DB/artifactdatabase/Webserverimportexportteststest_import.py:1: DeprecationWarning:在流响应上访问content属性已被弃用。请使用streaming_content属性。'

response.streaming_content返回某种映射,这绝对不是ZipFile所需要的文件类对象。我如何使用streaming_content属性?

顺便说一下,当我访问响应时,我只在将response.content传递给StringIO时得到弃用警告。

使用Python 3.4.

与字符串:

zip_content = io.StringIO("".join(response.streaming_content))
zip = ZipFile(zip_content)
与字节:

zip_content = io.BytesIO(b"".join(response.streaming_content))
zip = ZipFile(zip_content)

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

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

您可能希望通过检查response.streaming (boolean)来测试响应是否为流。

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

如果你想测试文件生成/完整性方法,你需要单独测试它的特性。对于Django Test来说,无论你的文件是ZIP还是CSV都没有关系,但是如果你调用它就可以了。

最新更新