我有一些python代码,它使用requests成功从URL下载图像,并将其保存到/tmp/
中。我想测试这是否应该。我使用responses来测试抓取JSON文件,但我不知道如何模拟抓取文件的行为。使用Python请求和响应模拟下载文件
我认为这将会是类似嘲讽一个标准的响应,如下面的,但我想我消隐如何设置body
是一个文件...
@responses.activate
def test_download():
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body='', status=200,
content_type='image/jpeg')
#...
UPDATE :继Ashafix的评论,我想这(蟒蛇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:试图跟随史蒂夫·杰索普的建议:
@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/
。
这有帮助吗? http://stackoverflow.com/a/26364642/2776376 –
'body = open(filename,'rb')。read()'不工作?在实践中,与任何“打开”调用一样,您可能希望将其用作上下文管理器。 –
谢谢双方......我已经提出了两个建议,但尚未完成! –