0

I have a reusable Django app and I'm importing it into another project. I want to be able to run the tests of that app which means I need my own settings.py file for just the tests.

What is the accepted way of running those tests? I've noticed some projects create a runtests.py file that adjusts the Django settings and uses the Django test runner.

I've also noticed that internal company projects tell the developer to create a new settings file, app_tests_settings.py and run the tests with manage.py:

./manage.py run tests --settings=app_tests_settings

Which way is best and are there other ways of running app-specific tests that use custom settings?

Update: the app needs a particular database backend because it uses PostGIS

1 Answers1

0

You can override settings in the setUp method of your TestCase.

https://docs.djangoproject.com/en/dev/topics/testing/overview/#overriding-settings

Updated.

Some code from the flatpages (https://github.com/django/django/blob/master/django/contrib/flatpages/tests/test_csrf.py):

from django.test.utils import override_settings


@override_settings(
    LOGIN_URL='/accounts/login/',
    MIDDLEWARE_CLASSES=(
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    ),
    TEMPLATE_DIRS=(
        os.path.join(os.path.dirname(__file__), 'templates'),
    ),
    SITE_ID=1,
)
class FlatpageCSRFTests(TestCase):
    fixtures = ['sample_flatpages', 'example_site']
    ...
Andrei Kaigorodov
  • 2,145
  • 17
  • 17