Django : How to upload csv file in unit test case using APIClient

I would like to write a unit test for a view on a Django REST Framework application. The test should upload a CSV file using the POST.

def _file_upload(client, string, args, file_name):
    base_path = os.path.dirname(os.path.realpath(__file__))
    with open(base_path + file_name, 'rb') as data:
        data = {
                'file': data
        response =, args=[args]), data, format = "multipart")
    return response.status_code,

The above code I used which obviously doesn't work it shows the following error

Missing filename. Request should include a Content-Disposition header with a filename parameter.

The following code is the one that I want to test via unit testing.

class ChartOfAccounts(views.APIView):
     parser_classes = (JSONParser, FileUploadParser)
     def post(self, request, pk, *args, **kwargs):
        csv_data = CSVUtils.format_request_csv(request.FILES['file'])
            coa_data = CSVUtils.process_chart_of_accounts_csv(company, csv_data)
            serializer = CoASerializer(coa_data, many=True)
            if len( > 0:
                return Utils.dispatch_success(request,

        except Exception as e:
            error = ["%s" % e]
            return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)

Any help regarding this is welcome. Thanks in advance

1 answer

  • answered 2018-05-16 09:22 Muthu Kumar

    I have fixed my issue using the different approach with HTTP headers HTTP_CONTENT_DISPOSITION, HTTP_CONTENT_TYPE by this reference

    And here is my code

    def _file_upload_csv( string, args, file_name):
        base_path = os.path.dirname(os.path.realpath(__file__))
        data = open(base_path + file_name, 'rb')
        data = SimpleUploadedFile(content =,name =,content_type='multipart/form-data')
        factory = RequestFactory()
        user = User.objects.get(username=UserConstant.ADMIN_USERNAME)
        view = ChartOfAccounts.as_view()
        content_type = 'multipart/form-data'
        headers= {
            'HTTP_CONTENT_TYPE': content_type,
            'HTTP_CONTENT_DISPOSITION': 'attachment; filename='+file_name}
        request =, args=[args]),{'file': data},
        force_authenticate(request, user=user)
        response = view(request, args)
        return response.status_code,

    **headers done the trick...