jeudi 25 avril 2019

POST multiple files using Django test client in same request

I am trying to build some tests for uploads on my Django site. It allows multiple files to be uploaded so I need to test when multiple files are uploaded.

Testing one file works great:

from django.test import Client

def test_stuff(self): 
    myfiles = open('....\file.csv','r') 
    c = Client()
    response = c.post('/', {'file':myfiles})

But trying it with a list of files doesn't work.

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        myfiles.append(open('....\file.csv','r'))
    c = Client()
    response = c.post('/', {'file':myfiles})

And neither does:

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        myfiles.append(open('....\file.csv','r'))
    c = Client()
    response = c.post('/', files={'file':myfiles})

or

def test_stuff(self): 
    file_list = # get list of file paths to open
    myfiles = []
    for file in file_list:
        myfiles.append(open('....\file.csv','r'))
    c = Client()
    response = c.post('/', files=myfiles)

My view gets the files from request.POST.get('myfiles'), but FILES is empty.

Is there a way to POST multiple files with django test client or should I use something else?

Aucun commentaire:

Enregistrer un commentaire