Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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 <code>git reset --hard</code>. 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. </p> <p>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 <a href="https://docs.python.org/3/library/unittest.mock.html" rel="nofollow noreferrer">Mock library</a> to completely fake the <code>File</code> instance (i.e. something like <code>mock_file = Mock(spec=django.core.files.File); mock_file.read.return_value = "fake file contents"</code>) so you can completely avoid changes to your file handling logic. The Mock library has a couple of ways to <a href="https://docs.python.org/3/library/unittest.mock.html#patch-object" rel="nofollow noreferrer">globally patch</a> Django's <a href="http://docs.djangoproject.com/en/dev/ref/files/file/" rel="nofollow noreferrer">File class</a> within a test method which is about as easy as this will get.</p> <p>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 <a href="http://docs.djangoproject.com/en/dev/ref/files/file/" rel="nofollow noreferrer">File object</a> after making sure it'll be stored somewhere appropriate - here are three ways to do that:</p> <ul> <li>Have your test <code>settings.MEDIA_ROOT</code> point to a temporary directory (see the Python <a href="http://docs.python.org/library/tempfile.html#tempfile.mkdtemp" rel="nofollow noreferrer">tempfile</a> module's <code>mkdtemp</code> function). This works fine as long as you have something like a separate <code>STATIC_ROOT</code> which you use for the media files which are part of your source code.</li> <li>Use a custom <a href="http://docs.djangoproject.com/en/dev/ref/files/storage/" rel="nofollow noreferrer">storage manager</a></li> <li>Set the file path manually on each File instance or have a custom <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to" rel="nofollow noreferrer">upload_to</a> function to point somewhere which your test setup/teardown process purges such as a test subdirectory under <code>MEDIA_ROOT</code>.</li> </ul> <p><strong>Edit:</strong> mock object library is new in python version 3.3. For older python versions check <a href="http://www.voidspace.org.uk/python/mock/" rel="nofollow noreferrer">Michael Foord's version</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload