Tuesday, August 29, 2017

Testing file upload via a form?

(Python 3.5 and Django 1.10)

I am trying to test a Django form that enables a required file upload.

The form is very simple and includes a field that looks like:

    upload_file = forms.FileField()

The corresponding test to try and check the upload:

def test_form_validation_with_file(self):
    fake_file = ContentFile(b'''Some file content''')
    fake_file.name = 'fake.xls'
    suf_file = SimpleUploadedFile('suf.xls', b'''this is text''')
    data = {
        'upload_file': suf_file,
    }
    form = forms.RequestForm(data=data)
    self.assertTrue(form.is_valid())

The form validation fails i.e. it does not seem to recognise that file data has been suppplied.

I have also tried with the  "fake_file" for the data but without success.

Any help or pointers appreciated.

Thanks
Derek

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3OTCbnw4o749YUz-Pa1-Uo9jkGkd1KybB5rSLnp3eAMDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment