New feature - Define a variable TESTS in settings

52 views
Skip to first unread message

אורי

unread,
Jul 1, 2019, 4:50:27 AM7/1/19
to Django developers (Contributions to Django itself)
Hi,

I think it might be useful to define a variable (such as TESTS) in settings, which will only be True when running tests, and then Django will know we are running tests and act accordingly, for example save files in a different directory (such as TESTS_MEDIA_ROOT) and not in the default MEDIA_ROOT directory, and delete them after all the tests end. And other actions which should run only in tests or only not in tests.

If such a variable is defined, its default value should be of course False.


Please see my recent comment on this ticket:
https://code.djangoproject.com/ticket/23251

I already defined different settings files in Speedy Net, and set set django_settings.TESTS to True only in tests. Until recently it was possible to run tests with other settings such as development, when running tests locally, but now it's deliberately disabled (with two asserts in speedy/core/base/test/models.py) because I don't want tests to run with the production (or development) MEDIA_ROOT settings.

Also notice, I used django_settings.TESTS_MEDIA_ROOT instead of django_settings.MEDIA_ROOT in tearDownClass in the above file, to prevent deleting the production MEDIA_ROOT directory by accident if the tests will still run in production (although it can't because of the asserts above).

I will appreciate your feedback or comments.

Rene Fleschenberg

unread,
Jul 1, 2019, 3:10:31 PM7/1/19
to django-d...@googlegroups.com
Hi,

I think it is better to do what Josh mentioned in
https://code.djangoproject.com/ticket/30605#comment:2.

To elaborate on how this looks in pratice:

Write a file called ``settings_test.py`` or such, containing something
like this:

```
from .settings import *

DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage'

# More test-specific settings here
```

And run your tests with this settings module (using ``manage.py
--settings settings_test test`` or by setting the
``DJANGO_SETTINGS_MODULE`` env var).

René

Reply all
Reply to author
Forward
0 new messages