Andrea
Andrea

Reputation: 20493

Django: change media root for tests

I am trying to write some tests for a Django application, involving file upload. For that, I want to change the MEDIA_ROOT temporarily to 'myproject/fixtures/test_media/'. For some reason it seems that this prevents Django to find the fixtures.

My test case looks like

from django.conf import settings

class TestMedia(TestCase):
    fixtures = ['fixtures/test_data.json']

    def setUp(self):
        settings.MEDIA_ROOT = ''.join(
            [settings.PROJECT_PATH, '/fixtures/test_media/'])

    def test_photo_size(self):
        pass # Actually do something with the media files

For some reason, Django cannot then load the fixtures, hence all tests fail with

Problem installing fixture 'fixtures/test_data.json': Traceback (most recent call last) ...

What am I doing wrong?

Upvotes: 7

Views: 4561

Answers (3)

Zemogle
Zemogle

Reputation: 614

You can override all the settings for tests. Its a more "Django" way of - the docs discourage directly altering settings. There is a section in the Django docs about it.

import os
from django.conf import settings
from django.test import TestCase, override_settings

@override_settings(MEDIA_ROOT=os.path.join(settings.PROJECT_PATH, 'fixtures/test_media/'))
class TestMedia(TestCase):
    fixtures = ['fixtures/test_data.json']

    def setUp(self):
         # any other setup stuff you need

A little bit more about overriding settings. You can use a decorator for a whole test:

from django.test import TestCase, override_settings


@override_settings(LOGIN_URL='/other/login/')
class LoginTestCase(TestCase):

    def test_login(self):
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/other/login/?next=/sekrit/')

or you can just override the setting for a portion of code:

from django.test import TestCase

class LoginTestCase(TestCase):

    def test_login(self):

        # First check for the default behavior
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/accounts/login/?next=/sekrit/')

        # Then override the LOGIN_URL setting
        with self.settings(LOGIN_URL='/other/login/'):
            response = self.client.get('/sekrit/')
            self.assertRedirects(response, '/other/login/?next=/sekrit/')

Note the different syntax for the inline version. Examples are taken from the docs.

Upvotes: 2

user2006656
user2006656

Reputation:

Not override in setUp, it is execute only before of run each test method of TestCase subclass, override it in module before all tests.

from django.conf import settings
settings.MEDIA_ROOT += 'test'

Upvotes: 3

LeafGlowPath
LeafGlowPath

Reputation: 3799

Have u tried to remove the setUp and see if the fixtures can be loaded?

Besides, I don't think you really need to change MEDIA_ROOT to test upload. You can just remove those uploaded files in tearDown to revert everything.

Edit:

It seems that you're running your unit test in production environment. Honestly, that's not a good idea.

But if u do have to do that, how about use another setting file? Like this:

from mysite.settings import *

MEDIA_ROOT = '/what_ever_you_like'

And run you test with an additional parameter:

./manage.py test --settings=mysite.settings_for_test

Upvotes: 3

Related Questions