pixel
pixel

Reputation: 26441

Django can't find settings file when running unit tests

I'm trying to run unit tests on my django application but I'm getting following error. Running application is fine though. I'm using pycharm 3.0.

C:\Users\user\my-project-env\Scripts\python.exe "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py" C:/Users/user/PycharmProjects/my-project/my_app/ true
Testing started at 20:16 ...
Traceback (most recent call last):
  File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 113, in <module>
    modules = loadModulesFromFolderRec(a[0])
  File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 63, in loadModulesFromFolderRec
    os.path.walk(folder, walkModules, (modules, pattern))
  File "C:\Users\user\my-project-env\lib\ntpath.py", line 259, in walk
    func(arg, top, names)
  File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 51, in walkModules
    modules.append(loadSource(os.path.join(dirname, name)))
  File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 40, in loadSource
    module = imp.load_source(moduleName, fileName)
  File "C:/Users/user/PycharmProjects/my-project/my_app/tests.py", line 8, in <module>
    from django.test import TestCase
  File "C:\Users\user\my-project-env\lib\site-packages\django\test\__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "C:\Users\user\my-project-env\lib\site-packages\django\test\client.py", line 21, in <module>
    from django.db import close_connection
  File "C:\Users\user\my-project-env\lib\site-packages\django\db\__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "C:\Users\user\my-project-env\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
    self._setup(name)
  File "C:\Users\user\my-project-env\lib\site-packages\django\conf\__init__.py", line 46, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Process finished with exit code 1

Upvotes: 6

Views: 6498

Answers (3)

TonyTony
TonyTony

Reputation: 1404

It is more due to the problem with settings in Pycharm, look at this question. Pycharm error: Improperly configured

Upvotes: 2

Tomas
Tomas

Reputation: 1725

Are you using Djangos unit test library, or are you using pythons? Django needs some things to be setup in order to be able to load e.g. models, i.e. it needs settings as your stack trace suggests.

If you follow the documentation https://docs.djangoproject.com/en/1.5/topics/testing/overview/ you can see that Django has done some addons to the normal unit test functionality. Using that and invoking the tests as Django unit tests will run it for you. Running it as Django unit tests means running them with

$ ./manage.py test

or since your using pycharm, you can select to run the unit test as a Django unit test, PyCharm should ask you when you invoke the test.

Upvotes: 6

akaRem
akaRem

Reputation: 7618

Look at Traceback:

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Upvotes: 5

Related Questions