Home » Django » how to unit test file upload in django

how to unit test file upload in django

Posted by: admin November 30, 2017 Leave a comment


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_key('file'):
        file = request.FILES['file']
        with open(settings.destfolder+'/%s' % file.name, 'wb+') as dest:
            for chunk in file.chunks():

I would like to unit test the view.I am planning to test the happy path as well as the fail path..ie,the case where the request.FILES has no key ‘file’ , case where request.FILES['file'] has None..

How do I set up the post data for the happy path?Can somebody tell me?


From Django docs on Client.post:

Submitting files is a special case. To POST a file, you need only
provide the file field name as a key, and a file handle to the file
you wish to upload as a value. For example:

c = Client()
with open('wishlist.doc') as fp:
  c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})


I used to do the same with open('some_file.txt') as fp: but then I needed images, videos and other real files in the repo and also I was testing a part of a Django core component that is well tested, so currently this is what I have been doing:

from django.core.files.uploadedfile import SimpleUploadedFile

def test_upload_video(self):
    video = SimpleUploadedFile("file.mp4", "file_content", content_type="video/mp4")
    self.client.post(reverse('app:some_view'), {'video': video})
    # some important assertions ...

In Python 3.5+ you need to use bytes object instead of str. Change "file_content" to b"file_content"

It’s been working fine, SimpleUploadedFile creates an InMemoryFile that behaves like a regular upload and you can pick the name, content and content type.


I recommend you to take a look at Django RequestFactory. IT’s the best way to mock data provided in the request.

Said that, I found several flaws in your code.

  • “unit” testing means to test just one “unit” of functionallity. So,
    if you want to test that view you’d be testing the view, and the file
    system, ergo, not really unit test. To make this point more clear. If
    you run that test, and the view works fine, but you don’t have
    permissions to save that file, your test would fail becouse of that.
  • Other important thing is test speed. If you’re doing something like
    TDD the speed of execution of your tests is really important.
    Accessing any I/O is not a good idea.

So, I recommend you to refactor your view to use a function like:

def upload_file_to_location(request, location=None): # Can use the default configured

And do some mocking on that. You can use Python Mock.

PS: You could also use Django Test Client But that would mean that you’re adding another thing more to test, becouse that client make use of Sessions, middleweres, etc. Nothing similar to Unit Testing.


I do something like this for my own event related application but you should have more than enough code to get on with your own use case

import tempfile, csv, os

class UploadPaperTest(TestCase):

    def generate_file(self):
            myfile = open('test.csv', 'wb')
            wr = csv.writer(myfile)
            wr.writerow(('Paper ID','Paper Title', 'Authors'))
            wr.writerow(('1','Title1', 'Author1'))
            wr.writerow(('2','Title2', 'Author2'))
            wr.writerow(('3','Title3', 'Author3'))

        return myfile

    def setUp(self):
        self.user = create_fuser()
        self.profile = ProfileFactory(user=self.user)
        self.event = EventFactory()
        self.client = Client()
        self.module = ModuleFactory()
        self.event_module = EventModule.objects.get_or_create(event=self.event,
        add_to_admin(self.event, self.user)

    def test_paper_upload(self):
        response = self.client.login(username=self.user.email, password='foz')

        myfile = self.generate_file()
        file_path = myfile.name
        f = open(file_path, "r")

        url = reverse('registration_upload_papers', args=[self.event.slug])

        # post wrong data type
        post_data = {'uploaded_file': i}
        response = self.client.post(url, post_data)
        self.assertContains(response, 'File type is not supported.')

        post_data['uploaded_file'] = f
        response = self.client.post(url, post_data)

        import_file = SubmissionImportFile.objects.all()[0]
        self.assertEqual(SubmissionImportFile.objects.all().count(), 1)
        #self.assertEqual(import_file.uploaded_file.name, 'files/registration/{0}'.format(file_path))

        file_path = import_file.uploaded_file.path


In Django 1.7 there’s an issue with the TestCase wich can be resolved by using open(filepath, ‘rb’) but when using the test client we have no control over it. I think it’s probably best to ensure file.read() returns always bytes.

source: https://code.djangoproject.com/ticket/23912, by KevinEtienne

Without rb option, a TypeError is raised:

TypeError: sequence item 4: expected bytes, bytearray, or an object with the buffer interface, str found