1

Question

I want to separate my prod settings from my local settings. I found this library django-split-settings, which worked nicely.

However somewhere in my code I have something like this:

if settings.DEBUG:

  # do debug stuff
else:
  # do prod stuff

The problem is that when i run my unit test command:

./run ./manage.py test

the above if statements evaluates settings.DEBUG as false. Which makes me wonder, which settings file is the test command reading from and how to correct it

What I have tried

I tried running a command like this:

./run ./manage.py test --settings=bx/settings

gives me this crash:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/beneple/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/beneple/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 302, in execute
    settings.INSTALLED_APPS
  File "/beneple/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/beneple/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/beneple/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: Import by filename is not supported.

any ideas?


update:

this is what my run command looks like

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
docker run \
  --env "PATH=/beneple/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
  -e "DJANGO_SETTINGS_MODULE=bx.settings.local" \
  --link beneple_db:db \
  -v $DIR:/beneple \
  -t -i --rm \
  beneple/beneple \
  $@

currently my manage.py looks like this

#!/usr/bin/env python
import os
import sys

    if __name__ == "__main__":

        from django.core.management import execute_from_command_line

        execute_from_command_line(sys.argv)

if I run this command:

./run ./manage.py shell

it works fine.. but for example when i try to run

./run ./flu.sh 

which in turn runs test_data.py which starts like so:

#!/usr/bin/env python
if __name__ == "__main__":
    import os, sys
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    import django
    django.setup()


..
from django.conf import settings
from settings import DOMAIN

it gives me the error:

Traceback (most recent call last):
  File "./bx/test_data.py", line 18, in <module>
    from settings import DOMAIN
ImportError: cannot import name DOMAIN
Done.

I'm not sure why that's happening, since my base.py definitely has DOMAIN set.

abbood
  • 23,101
  • 16
  • 132
  • 246
  • what is `./run`? Could you list your `settings.py` and your `manage.py`? --settings should bea module path like @Raja Simon mentioned – dm03514 Jul 24 '16 at 14:24
  • http://stackoverflow.com/questions/7447134/how-do-you-set-debug-to-true-when-running-a-django-test – dm03514 Jul 24 '16 at 14:31

2 Answers2

0

--settings is The Python path to a settings module. So bx is your root project folder and settings.py file is there then

./run ./manage.py test --settings="bx"

Raja Simon
  • 10,126
  • 5
  • 43
  • 74
  • doing that gives me this error: `django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.` see stack trace here > https://gist.github.com/abbood/163f4e73d0a8e54972e39bb30a157d2e – abbood Jul 24 '16 at 12:50
  • You must have to set SECRET_KEY .... Initially Django will not provide any secret key because that is sensitive.. so you have to provide that.. – Raja Simon Jul 24 '16 at 12:50
  • SECRET_KEY="something your key " – Raja Simon Jul 24 '16 at 12:52
  • i already set the secret_key inside my `settings.py` file.. does this mean it's trying to find it in another file? this line caught my attention ` File "/beneple/venv/local/lib/python2.7/site-packages/django/test/utils.py", line 139, in get_runner test_runner_class = settings.TEST_RUNNER`.. – abbood Jul 24 '16 at 12:53
  • does that mean i gotta create both `runtests.py` and `tests/test_settings.py` as instructed here https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications – abbood Jul 24 '16 at 12:53
  • Try setting `DJANGO_SETTINGS_MODULE` and don't give `--settings` – Raja Simon Jul 24 '16 at 13:01
  • Yeah try that `tests/test_settings.py` also – Raja Simon Jul 24 '16 at 13:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118130/discussion-between-abbood-and-raja-simon). – abbood Jul 24 '16 at 13:25
0

Django test, by default, set DEBUG = False

https://docs.djangoproject.com/en/1.9/topics/testing/overview/#other-test-conditions

dm03514
  • 54,664
  • 18
  • 108
  • 145