【发布时间】:2016-09-13 20:08:24
【问题描述】:
我有一些 python 代码可以使用requests 成功从 URL 下载图像,并将其保存到/tmp/。我想测试它应该做的事情。我正在使用responses 来测试获取 JSON 文件,但我不确定如何模拟获取文件的行为。
我认为这类似于模拟标准响应,如下所示,但我想我对如何将 body 设置为文件一无所知...
@responses.activate
def test_download():
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body='', status=200,
content_type='image/jpeg')
#...
更新:根据 Ashafix 的评论,我正在尝试这个(python 3):
from io import BytesIO
@responses.activate
def test_download():
with open('tests/images/tester.jpg', 'rb') as img1:
imgIO = BytesIO(img1.read())
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body=imgIO, status=200,
content_type='image/jpeg')
imgIO.seek(0)
#...
但是,随后,当我正在测试的代码尝试执行我得到的请求时:
a bytes-like object is required, not '_io.BytesIO'
感觉几乎是对的,但我很难过。
更新 2: 尝试遵循 Steve Jessop 的建议:
@responses.activate
def test_download():
with open('tests/images/tester.jpg', 'rb') as img1:
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body=img1.read(), status=200,
content_type='image/jpeg')
#...
但是这次被测试的代码提出了这个问题:
I/O operation on closed file.
图片当然应该在with 块内打开?
更新 3: 我正在测试的代码是这样的:
r = requests.get(url, stream=True)
if r.status_code == 200:
with open('/tmp/temp.jpg', 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
似乎最后的shutil 行正在生成“对已关闭文件的I/O 操作”。错误。我不太了解这一点 - 文件的流式传输 - 知道如何最好地模拟这种行为,测试下载的文件保存到 /tmp/。
【问题讨论】:
-
body = open(filename, 'rb').read()不起作用吗?在实践中,与对open的任何调用一样,您可能希望将其用作上下文管理器。 -
谢谢...我已经慢慢提出了这两个建议,但还没有完成!
-
你有没有试过不使用 with 块,然后简单地使用
img1 = open(...),然后使用img1.close()? -
感谢 Sepehr。我仍然收到“对已关闭文件的 I/O 操作”,即使我只是在调用我正在测试的代码后才关闭它。诡异的!可能与在请求中使用
stream有关吗?我在做r = requests.get(url, stream=True)
标签: python unit-testing python-requests