Django testing model with ImageField

I need to test the Photo model of my Django application. How can I mock the ImageField with a test image file?

class PhotoTestCase(TestCase):

    def test_add_photo(self):
        newPhoto = Photo()
        newPhoto.image = # ??????
        self.assertEqual(Photo.objects.count(), 1)


For future users, I've solved the problem. You can mock an ImageField with a SimpleUploadedFile instance.

from django.core.files.uploadedfile import SimpleUploadedFile

newPhoto.image = SimpleUploadedFile(name='test_image.jpg', content=open(image_path, 'rb').read(), content_type='image/jpeg')

Tell the mock library to create a mock object based on Django's File class

import mock
from django.core.files import File

file_mock = mock.MagicMock(spec=File, name='FileMock')

and then use in your tests

newPhoto.image = file_mock

You can use a temporary file, using tempfile. So you don't need a real file to do your tests.

import tempfile

image = tempfile.NamedTemporaryFile(suffix=".jpg").name

If you prefer to do manual clean-up, use tempfile.mkstemp() instead.


from StringIO import StringIO
# in python 3: from io import StringIO
from PIL import Image
from django.core.files.base import File

And create a static method in your TestCase class:

def get_image_file(name='test.png', ext='png', size=(50, 50), color=(256, 0, 0)):
    file_obj = StringIO()
    image ="RGBA", size=size, color=color), ext)
    return File(file_obj, name=name)


instance = YourModel(name=value, image=self.get_image_file())

If you don't want to create an actual file in the filesystem, you can use this 37-byte GIF instead, small enough to a be a bytes literal in your code:

from django.core.files.uploadedfile import SimpleUploadedFile

small_gif = (
uploaded = SimpleUploadedFile('small.gif', small_gif, content_type='image/gif')

My approach how to test model with no intention to pass any useful data:

from django.core.files import File

Need Your Help

Setting a WebRequest's body data

c# httpwebrequest

I'm creating a web request in ASP.NET and I need to add a bunch of data to the body. How do I do that?

Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default

Whenever I add a javascript or css file to my core project and I execute dotnet run in my bash terminal, I get the following error: