Je cette forme:Comment puis-je tester un formulaire avec FileField dans Django?
# forms.py
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['book_title', 'language', 'author', 'release_year', 'genre', 'ages', 'cover']
Type de champs: Où book_title
et author
sont CharField
, language
et genre
sont trop CharField
mais pour eux j'ai option de choix, release_year
et ages
sont IntegerField
, et cover
sont FileField
.
Options de Choice:
# models.py
ENGLISH = 'english'
LANGUAGE_CHOICES = (
(ENGLISH, 'English'),
)
ADVENTURE = 'adventure'
GENRE_CHOICES = (
(ADVENTURE, 'Adventure'),
)
maintenant: Je veux tester cette forme, mais je ne sais pas comment peut tester cover
, voici mon test de forme.
# test_forms.py
from .. import forms
from django.core.files import File
class TestBookForm:
def test_form(self):
form = forms.BookForm(data={})
assert form.is_valid() is False, 'Should be invalid if no data is given'
img = File(open('background'))
data = {'book_title': 'Lord Of The Rings',
'language': 'english',
'author': 'J. R. R. Tolkien',
'release_year': 1957,
'genre': 'adventure',
'ages': 16,
'cover': img}
form = forms.BookForm(data=data)
assert form.is_valid() is True
J'ai essayé: de django.core.files.uploadedfile importation SimpleUploadedFile
img = open('background')
uploaded = SimpleUploadedFile(img.name, img.read())
{'cover': uploaded}
Ceci est mon erreur:
E assert False is True
E + where False = <bound method BaseForm.is_valid of <BookForm bound=True, valid=False, fields=(book_title;language;author;release_year;genre;ages;cover)>>()
E + where <bound method BaseForm.is_valid of <BookForm bound=True, valid=False, fields=(book_title;language;author;release_year;genre;ages;cover)>> = <BookForm bound=True, valid=False, fields=(book_title;language;author;release_year;genre;ages;cover)>
.is_valid
REMARQUE: J'utilise Python 3.5
, Django 1.9.4
et je commence le test en utilisant py.test
.
MISE À JOUR: Si j'essaye open('background.jpg')
ne fonctionne pas. Erreur:FileNotFoundError: [Errno 2] No such file or directory: 'background.jpg'
-je résoudre ce
MISE À JOUR 2:
J'essaie d'utiliser mock
from django.core.files import File
import mock
file_mock = mock.MagicMock(spec=File, name='FileMock')
file_mock.name = 'test1.jpg'
{'cover': file_mock}
J'essaie d'utiliser InMemoryUploadedFile
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
from PIL import Image
im = Image.new(mode='RGB', size=(200, 200)) # create a new image using PIL
im_io = BytesIO() # a StringIO object for saving image
im.save(im_io, 'JPEG') # save the image to im_io
im_io.seek(0) # seek to the beginning
image = InMemoryUploadedFile(
im_io, None, 'random-name.jpg', 'image/jpeg', None, None
)
{'cover': image}
je fixe la chemin à mon image.
J'ai essayé de faire un faux fichier mais ne fonctionne pas, après avoir mis un fichier dans mon paquet et j'ai essayé de mettre sur le champ 'cover'. Ceci est ma dernière option? Mon fichier doit être 'jpg',' png' ou 'jpeg' – Sn0pY
Où avez-vous placé votre fichier background.jpg? Il doit être en place, d'où vous exécutez le test. Donc, si vous exécutez votre test à partir du dossier principal, le fichier background.jpg doit être présent – sebb
Mes 'test_forms' sont dans le paquet' tests', et mes 'background.jpg' sont dans le même paquet. Mais quand j'essaie 'open ('background.jpg')' ça ne marche pas ... mais 'open ('background')' fonctionne ... – Sn0pY