jpic
jpic

Reputation: 33420

How to add apps to INSTALLED_APPS for tests?

Currently, we can only run ./manage.py test autocomplete_light in the provided test_project because it has added them to INSTALLED_APPS.

Due to user demand, my goal for v2 was to enable tests in user projects. The first thing to do was to move example apps into a subdir of the module.

Now, all that I need is to be able to add them and their urls during test cases.

I tried overriding settings with the provided method but this overrides settings after the database was created.

Upvotes: 0

Views: 1271

Answers (2)

allcaps
allcaps

Reputation: 11248

Django provides the modify_settings() context manager for easier settings changes:

class TestSomeApp(TestCase):

    def test_some_app(self):
        with self.modify_settings(
            INSTALLED_APPS={"prepend": "some_app"}
        ):
            # ...

https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.modify_settings

Upvotes: 0

RemcoGerlich
RemcoGerlich

Reputation: 31270

You could run the tests with a special settings.py, by setting the DJANGO_SETTINGS_MODULE environment variable. This special "testsettings.py" can import * from the normal settings.py, and then add the necessary extra apps to INSTALLED_APPS.

Remember that settings.py files are just ordinary Python modules, so you can add any logic you want to get the list of extra apps from somewhere.

Upvotes: 1

Related Questions