Mocking a file storage backend in Django tests
It isn’t always obvious how and where to patch Django internals when (unit) testing your application.
If you have a model with a FileField and save an instance in a test, each time you run that test
a file will be written to your file system if you use the default FileSystemStorage
backend.
Consider the following example (Python 3.7 and Django 2.1 are used here):
To prevent a file from being saved on disk, or even uploaded to somewhere else if you use an alternative storage backend, you can use dj-inmemorystorage and configure that as your storage backend during testing. A downside is that this will not work if you have a specific FileField or ImageField that uses a different storage backend, for example S3.
Luckily, there is a better way by using the builtin unittest.mock
library (or the backported mock library on Python 2.7)
to patch the save
method of the storage class that is being used:
I have also stumbled on this blog post
that mentions patching the default storage backend, but that also won’t work if you have
set the storage
argument for your FileField or ImageField. For instance, if you’re
uploading files to S3 with the S3Boto3Storage
backend you can patch it directly: