繁体   English   中英

在没有外部文件的情况下使用ImageField对Django表单进行单元测试

[英]Unit Testing a Django Form with a ImageField without external file

Django版本:1.11,python版本:3.6.3

我发现了这个stackoverflow问题:

使用FileField对Django表单进行单元测试

而且我喜欢如何没有用于单元测试的实际图像/外部文件; 但是我尝试了以下方法:

from django.test import TestCase
from io import BytesIO
from PIL import Image
from my_app.forms import MyForm
from django.core.files.uploadedfile import InMemoryUploadedFile

class MyModelTest(TestCase):                    
    def test_valid_form_data(self):                    
        im_io = BytesIO() # BytesIO has to be used, StrinIO isn't working         
        im = Image.new(mode='RGB', size=(200, 200))         
        im.save(im_io, 'JPEG')                                  
        form_data = {
                'some_field': 'some_data'
            }                                               
        image_data = {   
                InMemoryUploadedFile(im_io, None, 'random.jpg', 'image/jpeg', len(im_io.getvalue()), None) 
            }    
        form = MyForm(data=form_data, files=image_data)             
        self.assertTrue(form.is_valid()) 

但是,这总是导致以下错误消息:

Traceback (most recent call last):
  File "/home/my_user/projects/my_app/products/tests/test_forms.py", line 44, in test_valid_form_data
    self.assertTrue(form.is_valid())
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 175, in errors
    self.full_clean()
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 384, in full_clean
    self._clean_fields()
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 396, in _clean_fields
    value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/widgets.py", line 423, in value_from_datadict
    upload = super(ClearableFileInput, self).value_from_datadict(data, files, name)
  File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/widgets.py", line 367, in value_from_datadict
    return files.get(name)
AttributeError: 'set' object has no attribute 'get'

为什么? 我知道.get()是一个字典方法,但是我看不到它是在哪里创建集合的。

image_data应该是dict,而不提供键{value}将创建集合对象。 您需要像这样定义{key: value} 解决此问题:

image_data = {   
            'image_field': InMemoryUploadedFile(im_io, None, 'random.jpg', 'image/jpeg', len(im_io.getvalue()), None) 
        }   

没有外部文件代码

from django.core.files.uploadedfile import SimpleUploadedFile
testfile = (
b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00\x00\x00\x21\xf9\x04'
b'\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02'
b'\x02\x4c\x01\x00\x3b')

avatar = SimpleUploadedFile('small.gif', testfile, content_type='image/gif')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM