I have a model with a FileField. I want to unittest it. django test framework has great ways to manage database and emails. Is there something similar for FileFields?
How can I make sure that the unittests are not going to pollute the real application?
Thanks in advance
PS: My question is almost a duplicate of Django test FileField using test fixtures but it doesn't have an accepted answer. Just want to re-ask if something new on this topic.
Django provides a great way to do this - use a SimpleUploadedFile
or a TemporaryUploadedFile
. SimpleUploadedFile
is generally the simpler option if all you need to store is some sentinel data:
from django.core.files.uploadedfile import SimpleUploadedFile my_model.file_field = SimpleUploadedFile( "best_file_eva.txt", b"these are the file contents!" # note the b in front of the string [bytes] )
It's one of django's magical features-that-don't-show-up-in-the-docs :). However it is referred to here and implemented here.
Note that you can only put bytes
in a SimpleUploadedFile
since it's implemented using BytesIO
behind the scenes. If you need more realistic, file-like behavior you can use TemporaryUploadedFile
.
If you're stuck on python 2, skip the b
prefix in the content:
my_model.file_field = SimpleUploadedFile( "best_file_eva.txt", "these are the file contents!" # no b )
There are several ways you could tackle this but they're all ugly since unit tests are supposed to be isolated but files are all about durable changes.
My unit tests don't run on a system with production data so it's been easy to simply reset the upload directory after each run with something like git reset --hard
. This approach is in some ways the best simply because it involves no code changes and is guaranteed to work as long as you start with good test data.
If you don't actually need to do anything with that file after testing your model's save method, I'd recommend using python's excellent Mock library to completely fake the File
instance (i.e. something like mock_file = Mock(spec=django.core.files.File); mock_file.read.return_value = "fake file contents"
) so you can completely avoid changes to your file handling logic. The Mock library has a couple of ways to globally patch Django's File class within a test method which is about as easy as this will get.
If you need to have a real file (i.e. for serving as part of a test, processing with an external script, etc.) you can use something similar to Mirko's example and create a File object after making sure it'll be stored somewhere appropriate - here are three ways to do that:
settings.MEDIA_ROOT
point to a temporary directory (see the Python tempfile module's mkdtemp
function). This works fine as long as you have something like a separate STATIC_ROOT
which you use for the media files which are part of your source code.MEDIA_ROOT
.Edit: mock object library is new in python version 3.3. For older python versions check Michael Foord's version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With