0

The following code causes problems, If I add in my unit test file in Django :

from myApp.models import anyModel

Failed to import test module: test_App.test_models Traceback (most recent call last):

File "C:\Program Files (x86)\Python38-32\lib\unittest\loader.py", line 436, in _find_test_path

module = self._get_module_from_name(name)

File "C:\Program Files (x86)\Python38-32\lib\unittest\loader.py", line 377, in _get_module_from_name
__import__(name)

File "d:\SC\Top4Cast\Top4CastsApp\test_App\test_models.py", line 3, in <module>
from Top4CastsApp.models import Author


File "d:\SC\Top4Cast\Top4CastsApp\models.py", line 31, in <module>
class Diagram(models.Model):


File "d:\SC\Top4Cast\env\lib\site-packages\django\db\models\base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)

File "d:\SC\Top4Cast\env\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config

self.check_apps_ready()

File "d:\SC\Top4Cast\env\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
  File "d:\SC\Top4Cast\env\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
  File "d:\SC\Top4Cast\env\lib\site-packages\django\conf\__init__.py", line 63, in _setup

raise ImproperlyConfigured(

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.

1 Answers1

1

After struggling for a while I found the reason and fixed it. Let first, explain the problem:

1- what is the bug situation:

You have used Django+VSCode for your web project and want to get benefit of using vscode-test-panel for discovering your tests and probably debug your tests separately by using test explorer. This is the web reference for handle this job:

If you follow the steps all is fine but as long as you import a model from your models.py like this:

  • from myApp.models import anyModel

your test discovery will face the following error:

raise ImproperlyConfigured( 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.

2-What is the bug for:

If you face this error you can see that this command line command is working fine:

  • python manage.py test

The problem is related to setting DJANGO_SETTINGS_MODULE.

3-What is the solution:

I found the fix like thius:

1- using pytest framework instead of unit test framework

2- pip install pytest-django

3- create pytest.ini and put it in the root of your project

4- add the following content in pytest.ini:

-- FILE: pytest.ini (or tox.ini)

[pytest]

DJANGO_SETTINGS_MODULE = mysite.settings (mysite shoul be change to your web project name)

# -- recommended but optional:

python_files = tests.py test_*.py *_tests.py

the last line will fix the issue.

I have used the following references: