For someone to try upload-image test with python 3.xx
I fix little with Maxim Panfilov's excellent answer to make more dummy image with independent name.
from io import BytesIO
from PIL import Image
from django.core.files.base import File
#in your TestCase class:
class TestClass(TestCase):
@staticmethod
def get_image_file(name, ext='png', size=(50, 50), color=(256, 0, 0)):
file_obj = BytesIO()
image = Image.new("RGBA", size=size, color=color)
image.save(file_obj, ext)
file_obj.seek(0)
return File(file_obj, name=name)
def test_upload_image(self):
c= APIClient()
image1 = self.get_image('image.png')
image2 = self.get_image('image2.png')
data =
{
"image1": iamge1,
"image2": image2,
}
response = c.post('/api_address/', data )
self.assertEqual(response.status_code, 201)