3

I have a draft of the Django project with the added one application (e.g. my_app). In this app I have places tests.py file with one test:

import unittest

class Test_MyModel(unittest.TestCase):
  def test_dummy(self):
    self.assertEqual(1,1)

In this case, this dummy test is discoverd by Visual Studio Code and could be executed in it, also it's possible to launch this test from command line:

python manage.py test

When I modify my file with unit test add import some model (placed in file models.py) from my_app:

import unittest

from .models import MyModel # new added line

class Test_MyModel(unittest.TestCase):
  def test_dummy(self):
    self.assertEqual(1,1)

In this case I'm still be able run test from command line without any problems, but VSC doesn't discover my test and in the Python Test Log in the VSC I get error:

======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 434, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 375, in _get_module_from_name
__import__(name)
File "/Users/myuser/Projects/backend/my_app/tests.py", line 3, in <module>
from my_app.models import MyModel
File "/Users/myuser/Projects/backend/my_app/models.py", line 4, in <module>
class MyModel(models.Model):
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 64, 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.

Any hints or advice how can I resolve this problem and start fully using unit test under VSC for models test?

1 Answer 1

4

I've found a solution to my problem. I need to add below lines into my init.py file in my_app folder.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app_project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Sign up to request clarification or add additional context in comments.

1 Comment

This solution also resolved my issues: (1) Django unit tests weren't discoverable in Visual Studio Code; or (2) I got a "TypeError: argument of type 'ConnectionHandler' is not iterable" message if they were discoverable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.