how to unit test file upload in django

后端 未结 11 1047
感动是毒
感动是毒 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条回答
  •  遥遥无期
    2020-12-02 07:43

    from rest_framework.test import force_authenticate
    from rest_framework.test import APIRequestFactory
    
    factory = APIRequestFactory()
    user = User.objects.get(username='#####')
    view = .as_view()
    with open('.pdf', 'rb') as fp:
        request=factory.post('',{'file_name':fp})
    force_authenticate(request, user)
    response = view(request)
    

提交回复
热议问题