how to unit test file upload in django

后端 未结 11 1071
感动是毒
感动是毒 2020-12-02 07:09

In my django app, I have a view which accomplishes file upload.The core snippet is like this

...
if  (request.method == \'POST\'):
    if request.FILES.has_         


        
11条回答
  •  萌比男神i
    2020-12-02 07:36

    from django.test import Client
    from requests import Response
    
    client = Client()
    with open(template_path, 'rb') as f:
        file = SimpleUploadedFile('Name of the django file', f.read())
        response: Response = client.post(url, format='multipart', data={'file': file})
    

    Hope this helps.

提交回复
热议问题