在同一请求中使用Django测试客户端发布多个文件

时间:2019-04-25 21:17:56

标签: python django testing post integration-testing

我正在尝试为Django站点上的上传构建一些测试。它允许上传多个文件,因此我需要测试何时上传多个文件。

测试一个文件效果很好:

from django.test import Client

def test_stuff(self): 
    with open('....\file.csv','rb') as fp:
        c = Client()
        response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file': fp})

但是尝试使用文件列表是行不通的。

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        with open('....\file.csv','rb') as fp:
            myfiles.append(fp)
    c = Client()
    response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file':myfiles})

而且也不是:

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        with open('....\file.csv','rb') as fp:
            myfiles.append(fp)
    c = Client()
    response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com',}, files={'file':myfiles})

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        with open('....\file.csv','rb') as fp:
            myfiles.append(fp)
    c = Client()
    response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com'}, files=myfiles)

我的视图从request.POST.get('myfiles')获取文件,但是FILES为空。

是否可以使用Django测试客户端发布多个文件,还是应该使用其他内容?

  

进行编辑以提高准确性

2 个答案:

答案 0 :(得分:0)

部分问题是使用with时,文件立即关闭并退出语句。毫不奇怪,另一部分是以正确的格式获取数据的。 Django的测试客户端希望所有数据都作为字典,因此,由于我还发送了用户名和电子邮件,因此需要将其格式设置为:

def test_stuff(self): 
    file_list = # get list of file paths to open
    data = {}
    files = []
    for file in file_list:
        fp = open('....\file.csv','rb')
        files.append(fp)
    data['myfiles'] = files
    data['name'] = 'Some Name'
    data['email'] = 'some@email.com'
    c = Client()
    response = c.post('/', data)

答案 1 :(得分:0)

此外,如果您使用 SimpleUploadedFile,您可以传递多个文件:

file1 = SimpleUploadedFile('file1.txt', b'file-1')
file2 = SimpleUploadedFile('file2.txt', b'file-2')

response = self.client.post('some url', data={
  'file': [ file1, file2]
})

在视图中它可能是这样的:

class UploadFormView(FormView):
    template_name = '...'
    form_class = YourForm

    # We require to override the post method to manage multiple files.
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file')
        if form.is_valid():
            for f in files:
              # do smth with file
            return self.form_valid(form)
        else:
            return self.form_invalid(form)