title: Unit testing Django model with an image - Not quite understanding SimpleUploadedFile
body: <p>I'm a testing noob and I'm trying to figure out how to write a test to confirm that a model form is valid and will generate a new instance of <code>Post</code>, which is a model that has an image field.</p>
  <p>I looked <a href="https://stackoverflow.com/questions/26298821/django-testing-model-with-imagefield">some other</a> SO posts, and it looks like I should be using SimpleUploadedFile to mock the image field. I'm having a hard time understanding how SimpleUploadedFile works (haven't found any straightforward documentation for this application), and different SO posts use some different looking syntax.</p>
  <p>Am I supposed to point to a real file path to an actual image that is held somewhere in my Django app, or does this create a fake file to be used?</p>
  <p>tests.py</p>
  <pre><code>class CreatePost(TestCase):
      def test_create_post(self):
          data = {
              &quot;content&quot;: &quot;This is a post, I'm testing it out&quot;
          }
          files_data = {
              &quot;image&quot;: SimpleUploadedFile(name='test_image.jpg', content=open(image_path, 'rb').read(), content_type='image/jpeg') 
          }
          response = self.client.post(&quot;/new&quot;, data=data, files=files_data)
          self.assertEqual(Post.objects.count(),1)
          self.assertRedirects(response, 'index')
  </code></pre>
  <p>models.py</p>
  <pre><code>class Post(models.Model):
      content = models.CharField(max_length=260)
      timestamp = models.DateTimeField(auto_now_add=True)
      posted_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name=&quot;posts&quot;)
      liked_by = models.ManyToManyField(User, blank=True, related_name=&quot;liked_posts&quot;)
      image = models.ImageField(upload_to='uploads/', verbose_name='image')
      def __str__(self):
          return f&quot;{self.posted_by} posted {self.content} at {self.timestamp}&quot;
      def is_valid_post(self):
          return len(self.content) &lt;= 260 and len(self.content) &gt;= 0
       
  
  class Post_form(ModelForm):
      class Meta: 
          model = Post
          fields = ['content', 'image']
  </code></pre>