Django test FileField using test fixtures

Django provides a great way to write tests on FileFields without mucking about in the real filesystem – use a SimpleUploadedFile.

from django.core.files.uploadedfile import SimpleUploadedFile

my_model.file_field = SimpleUploadedFile('best_file_eva.txt', b'these are the contents of the txt file')

It’s one of django’s magical features-that-don’t-show-up-in-the-docs :). However it is referred to here.

Leave a Comment