I have pytest-django == 2.9.1 installed I started setting up a test environment according to the instructions. https://pytest-django.readthedocs.io/en/latest/tutorial.html#step-2-point-pytest-to-your-django-settings
In the second step, in the root of the project, I created a pytest.ini file and added DJANGO_SETTINGS_MODULE there (all according to the instructions) But when you start the test, an error appears
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
But DJANGO_SETTINGS_MODULE is defined in the pytest.ini! I tried putting pytest.ini in different directories, but the result is always the same. I tried running with a parameter --ds, but the result is still the same. Can you tell me why this happens?
UPD
Hmm... I remove [pytest] from the file header and got an error "no section header defined" The file does not seem to be ignored, but DJANGO_SETTINGS_MODULE is still not exported.
I tried adding it to the environment variables separately and received an error on startup
export DJANGO_SETTINGS_MODULE=project.settings.test_settings
pytest
ImportError: Could not import settings 'project.settings.test_settings' (Is it on sys.path? Is there an import error in the settings file?): No module named project.settings.test_settings
But at startup python manage.py runserver --settings=project.settings.test_settings everything is working fine
had the same issue, there were 2 problems:
So ensure you can import settings.py as hoefling recommended, and ensure pytest-django is actually installed in your environment
So the docs say that the order of precedence when choosing setting is
command line environment variable pytest.ini file.
Then it goes further to say you can override this precedence using addopts
.
In my case, I specified my settings like below
DJANGO_SETTINGS_MODULE=bm.settings.ttest
addopts = --ds=bm.settings.ttest
And it works.
If 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