Search code examples
djangopytestdjango-testingdjango-channelsdjango-tests

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:

  • place django.conf.settings.configure() at the top of the test script
  • place the following code at the top of the test script

    ROOT_DIR = environ.Path(file) - 2

    env = environ.Env()

    env_file = str(ROOT_DIR.path('.env'))

    env.read_env(env_file)


Solution

  • 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