本文介绍了如何测试send_file烧瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小瓶应用程序,需要一些图片上传,并将其转换成多页TIFF。没什么特别的。

但是我怎么测试上传的多个文件和这个文件的下载呢?

我的Testclient:

  class RestTestCase(unittest.TestCase):
def setUp(self):
self.dir = os.path.dirname(__ file__)
rest = imp.load_source('rest',self.dir +'/../rest.py')
rest。 app.config ['TESTING'] = True
self.app = rest.app.test_client()

run run(self):
with open(self.dir + 'img1:
img1StringIO = StringIO(img1.read())

response = self.app.post('/ convert'('/img/img1.jpg','rb' ,
content_type ='multipart / form-data',
data = {'photo':(img1StringIO,'img1.jpg')},
follow_redirects = True)
assert True

if __name__ ==__main__:
单位test.main()

应用程序通过

  return send_file(result,mimetype ='image / tiff',\ 
as_attachment = True)

我想读取响应中发送的文件,并将其与另一个文件进行比较。我怎么从响应对象中获取文件?

解决方案

我想也许这里的混乱是响应是一个对象,而不是通过发布请求下载的数据。这是因为HTTP响应具有其他常常有用的知识,例如返回的http状态代码,响应的MIME类型等等。访问这些参数的属性名称在上面的链接中列出。响应对象有一个名为'data'的属性,所以 response.data 将包含从服务器下载的数据。我链接到的文档表明很快就会弃用 data ,而应该使用 get_data()方法,但仍然使用数据。测试你自己的系统,看看有什么作用。假设你想测试一个数据的往返,

  def runTest(self ):
with open(self.dir +'/img/img1.jpg','rb')as img1:
img1StringIO = StringIO(img1.read())

response = self.app.post('/ convert',
content_type ='multipart / form-data',
data = {'photo':(img1StringIO,'img1.jpg')},
follow_redirects = True)
img1StringIO.seek(0)
assert response.data == imgStringIO.read()


I have a small flask application which takes some images for upload and converts them into a multipage tiff. Nothing special.

But how do I test the upload of multiple files and the file download?

My Testclient:

class RestTestCase(unittest.TestCase):
    def setUp(self):
        self.dir = os.path.dirname(__file__)
        rest = imp.load_source('rest', self.dir + '/../rest.py')
        rest.app.config['TESTING'] = True
        self.app = rest.app.test_client()

    def runTest(self):
        with open(self.dir + '/img/img1.jpg', 'rb') as img1:
            img1StringIO = StringIO(img1.read())

        response = self.app.post('/convert',
                                 content_type='multipart/form-data',
                                 data={'photo': (img1StringIO, 'img1.jpg')},
                                 follow_redirects=True)
        assert True

if __name__ == "__main__":
    unittest.main()

The application sends back the file with

return send_file(result, mimetype='image/tiff', \
                                     as_attachment=True)

I want to read the file sent in the response and compare it with another file. How do I get the file from the response object?

解决方案

I think maybe the confusion here is that response is a Response object and not the data downloaded by the post request. This is because an HTTP response has other attributes that are often useful to know, for example http status code returned, the mime-type of the response, etc... The attribute names to access these are listed in the link above.

The response object has an attribute called 'data', so response.data will contain the data downloaded from the server. The docs I linked to indicate that data is soon to be deprecated, and the get_data() method should be used instead, but the testing tutorial still uses data. Test on your own system to see what works.Assuming you want to test a round trip of the data,

def runTest(self):
    with open(self.dir + '/img/img1.jpg', 'rb') as img1:
        img1StringIO = StringIO(img1.read())

    response = self.app.post('/convert',
                             content_type='multipart/form-data',
                             data={'photo': (img1StringIO, 'img1.jpg')},
                             follow_redirects=True)
    img1StringIO.seek(0)
    assert response.data == imgStringIO.read()

这篇关于如何测试send_file烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:00