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 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)
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
For all of you who haven't fixed this yet, this problem in my experience happens in one of this 3 scenarios:
pip install pytest-django
firstDJANGO_SETTINGS_MODULE
variable on your pytest.ini
or setup.cfg
filepytest
command on the wrong directory. Make sure you run it where your pytest.ini
/setup.cfg
file isIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With