简体   繁体   English

在pytesting中,如何提供文件/图像的链接/指针以将图像上传到flask

[英]In pytesting, how to provide the link/pointer to the file/image for uploading the image in flask

I am using flask with python 3.5 and try to test my code with pytest for uploading the image.I have gone through various answers but sadly couldn't get the point to solve my problem.In link of github, it explains how to use filename, file_field https://gist.github.com/DazWorrall/1779861 I tried in that way also but i wasn't going in right direction. 我在python 3.5中使用flask,并尝试使用pytest测试我的代码以上传图像。我经历了各种答案,但可惜无法解决我的问题。在github的链接中,它解释了如何使用文件名,file_field https://gist.github.com/DazWorrall/1779861我也尝试过这种方式,但是我的方向不正确。 Please help me in solving my problem. 请帮助我解决我的问题。 Here, post method is used to upload the image, content-part should be multi-part but regarding data, how to send the image data and its path. 在这里,使用post方法上传图像,内容部分应该是多部分,但是关于数据,如何发送图像数据及其路径。

test_client.post(
    '/uploadimage',
    content_type='multipart/form-data',
    buffered=True,
    data=dict(
        file='user4.jpg', file_field=io.BytesIO(b"~/Downloads/images/")),
    follow_redirects=True)

In here, while running for pytest it doesn't recognise the file. 在这里,运行pytest时无法识别该文件。 Dont know why? 不知道为什么? I will hope to get my answer soon.. Thanks. 我希望能尽快得到答案。。谢谢。

res = test_client.post( products_url, content_type='multipart/form-data', buffered=True, data={ 'file': (io.BytesIO(b'~/Downloads/images'), 'user4.jpg'), }) res = test_client.post(products_url,content_type ='multipart / form-data',buffered = True,data = {'file':(io.BytesIO(b'〜/ Downloads / images'),'user4.jpg') ,})

Little bit change the way data is send. 一点点改变了数据的发送方式。 It is working fine for me. 对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM