Unit Testing a Django Form Using FileField

I have a form:

#forms.py from django import forms class MyForm(forms.Form): title = forms.CharField() file = forms.FileField() #tests.py from django.test import TestCase from forms import MyForm class FormTestCase(TestCase) def test_form(self): upload_file = open('path/to/file', 'r') post_dict = {'title': 'Test Title'} file_dict = {} #?????? form = MyForm(post_dict, file_dict) self.assertTrue(form.is_valid()) 

How do I create a diskfile to submit the upload_file to the form?

+47
django unit-testing django-forms
Mar 18 '10 at 21:16
source share
4 answers

So far I have found this method that works

 from django.core.files.uploadedfile import SimpleUploadedFile ... def test_form(self): upload_file = open('path/to/file', 'rb') post_dict = {'title': 'Test Title'} file_dict = {'file': SimpleUploadedFile(upload_file.name, upload_file.read())} form = MyForm(post_dict, file_dict) self.assertTrue(form.is_valid()) 
+79
Mar 18
source share

This may not be entirely correct, but I am creating an image file in unit test using StringIO:

 imgfile = StringIO('GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00' '\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;') imgfile.name = 'test_img_file.gif' response = self.client.post(url, {'file': imgfile}) 
+21
Mar 18 '10 at 21:25
source share

Here is another way that does not require the use of a real image.

EDIT: Updated for Python 3.

 from PIL import Image from io import BytesIO # Python 2: from StringIO import StringIO from django.core.files.uploadedfile import InMemoryUploadedFile ... def test_form(self): im = Image.new(mode='RGB', size=(200, 200)) # create a new image using PIL im_io = BytesIO() # a BytesIO object for saving image im.save(im_io, 'JPEG') # save the image to im_io im_io.seek(0) # seek to the beginning image = InMemoryUploadedFile( im_io, None, 'random-name.jpg', 'image/jpeg', len(im_io.getvalue()), None ) post_dict = {'title': 'Test Title'} file_dict = {'picture': image} form = MyForm(data=post_dict, files=file_dict) 
+5
Dec 14 '15 at 21:29
source share

Combining several ideas from this and other publications on the stack using Python 3. No external files or libraries are required.

 from django.core.files.uploadedfile import SimpleUploadedFile png_hex = ['\x89', 'P', 'N', 'G', '\r', '\n', '\x1a', '\n', '\x00', '\x00', '\x00', '\r', 'I', 'H', 'D', 'R', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x01', '\x08', '\x02', '\x00', '\x00', '\x00', '\x90', 'w', 'S', '\xde', '\x00', '\x00', '\x00', '\x06', 'b', 'K', 'G', 'D', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xf9', 'C', '\xbb', '\x7f', '\x00', '\x00', '\x00', '\t', 'p', 'H', 'Y', 's', '\x00', '\x00', '\x0e', '\xc3', '\x00', '\x00', '\x0e', '\xc3', '\x01', '\xc7', 'o', '\xa8', 'd', '\x00', '\x00', '\x00', '\x07', 't', 'I', 'M', 'E', '\x07', '\xe0', '\x05', '\r', '\x08', '%', '/', '\xad', '+', 'Z', '\x89', '\x00', '\x00', '\x00', '\x0c', 'I', 'D', 'A', 'T', '\x08', '\xd7', 'c', '\xf8', '\xff', '\xff', '?', '\x00', '\x05', '\xfe', '\x02', '\xfe', '\xdc', '\xcc', 'Y', '\xe7', '\x00', '\x00', '\x00', '\x00', 'I', 'E', 'N', 'D', '\xae', 'B', ''', '\x82'] valid_png_bin = str.encode("".join(png_hex)) png = SimpleUploadedFile("test.png", valid_png_bin) post_dict = {'title': 'Test Title'} file_dict = {'picture': png} form = MyForm(data=post_dict, files=file_dict) 
0
Jun 26 '19 at 19:20
source share



All Articles