Bill Vergara
Bill Vergara

Reputation: 154

Django channels pytest testing. django.core.exceptions.ImproperlyConfigured. .

I am getting this error when running pytest. I am following this tutorial: https://channels.readthedocs.io/en/latest/topics/testing.html

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.

The following the things I've tried:

Upvotes: 8

Views: 9470

Answers (2)

Lucas Miguel
Lucas Miguel

Reputation: 562

For all of you who haven't fixed this yet, this problem in my experience happens in one of this 3 scenarios:

  • Pytest can't understand Django's structure on its own, so you'll need to pip install pytest-django first
  • You haven't properly configured your DJANGO_SETTINGS_MODULE variable on your pytest.ini or setup.cfg file
  • You are running the pytest command on the wrong directory. Make sure you run it where your pytest.ini/setup.cfg file is

Upvotes: 12

AzMoo
AzMoo

Reputation: 506

You will need to configure pytest to configure the django settings in your pytest.ini as documented here: https://pytest-django.readthedocs.io/en/latest/

# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = test_settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py

Upvotes: 14

Related Questions