0

With PyCharm IDE, we have a unit test importing the models module of the Django project for testing. While expecting a happy pass, the test always hit en error saying:

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

We are new to this part of the Python unit test, so we highly appreciate any hints or suggestions.

The unit test:

from applicationapp import models
...

class TestElastic(TestCase):
    def test_parse_request(self):
        # expecting happy pass
        # 
        # self. Fail()
        return

The full error trace:

/data/app-py3/venv3.7/bin/python /var/lib/snapd/snap/pycharm-professional/327/plugins/python/helpers/pycharm/_jb_unittest_runner.py --target test_elastic.TestElastic 
Testing started at 2:07 PM ...
Launching unittests with arguments python -m unittest test_elastic.TestElastic in /data/app-py3/APPLICATION/tests

Traceback (most recent call last):
  File "/var/lib/snapd/snap/pycharm-professional/327/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING))
  File "/usr/local/lib/python3.7/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python3.7/unittest/main.py", line 147, in parseArgs
    self.createTests()
  File "/usr/local/lib/python3.7/unittest/main.py", line 159, in createTests
    self.module)
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/data/app-py3/APPLICATION/tests/test_elastic.py", line 6, in <module>
    from applicationapp.soap.elastic import Elastic
  File "/data/app-py3/APPLICATION/applicationapp/soap/elastic.py", line 7, in <module>
    from applicationapp import models
  File "/data/app-py3/APPLICATION/applicationapp/models.py", line 3, in <module>
    from django.contrib.auth.models import User
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 68, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, 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

Empty suite

James
  • 1,373
  • 3
  • 13
  • 27

2 Answers2

1

CD to your project directory and use

python manage.py test

See the docs here:

https://docs.djangoproject.com/en/4.2/topics/testing/overview/#running-tests

Alternatively, you can set the DJANGO_SETTINGS_MODULE as an environment variable. This step really depends on the environment you're working in, but to get it working and send you on the right path, you would likely want to do something like this in a terminal:

export DJANGO_SETTINGS_MODULE=dot.path.to.settings.file

Please note, the dot path is relative to the project, so if your main app is called "polls" then you're dot variable might be polls.settings

I had a closer read of your traceback and I could be wrong, so please do correct me but I believe the dot path would be applicationapp.settings

Swift
  • 1,663
  • 1
  • 10
  • 21
1

Following the hint from this post, we resolved the issue by explicitly calling django.setup() at the beginning of the file. See the sample source code below.

from unittest import TestCase

...

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "applicationapp.settings")
django.setup()
from applicationapp import models

...

class TestElastic(TestCase):
    def test_parse_request(self):
        # expecting happy pass
        # 
        # self. Fail()
        return
James
  • 1,373
  • 3
  • 13
  • 27
  • Cant say I've ever done it this way, and I can imagine it might have to be replicated for other tests which seems like bad practice. – Swift Jun 01 '23 at 15:43