Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active August 29, 2015 13:59
Show Gist options
  • Save cansadadeserfeliz/10480812 to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/10480812 to your computer and use it in GitHub Desktop.
Django: ImageField in unittests
class Post(models.Model):
image = models.ImageField(
max_length=255,
upload_to='uploads/blog',
)
from django.test import TestCase
from django.core.urlresolvers import reverse
from blog.models import Post
from StringIO import StringIO
from PIL import Image
class BlogPostTestCase(TestCase):
def test_admin(self):
file_obj = StringIO()
image = Image.new('RGBA', size=(50, 50))
image.save(file_obj, 'png')
file_obj.name = 'test.png'
file_obj.seek(0)
self.client.post(
reverse('admin:post_add'),
{
'title': 'xxx',
'image': file_obj,
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment