[Django]-During tests, how to override a Django setting used in the urlconf?

4๐Ÿ‘

โœ…

As you guessed, you have overriden settings after they have already been imported in urls. This would have worked if you had used the settings variable inside some method, and not in global scope of urls.py.

Although overriding settings configuration on runtime might seem convenient, in my opinion, you should create a separate file for testing. This saves a lot of configuration for testing and this would ensure that you never end up doing something irreversible (like cleaning staging database).

# test_settings.py

MY_ROOT_DIR = 'terry'

Say your testing file exists in โ€˜my_project/test_settings.pyโ€™, add

settings = 'my_project.test_settings' if 'test' in sys.argv else 'my_project.settings'

in your manage.py. This will ensure that when you run python manage.py test you use test_settings only. If you are using some other testing client like pytest, you could as easily add this to pytest.ini

๐Ÿ‘คhspandher

Leave a comment