Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest ignores pytest.ini file

Tags:

pytest

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

like image 868
Richard Davis Avatar asked Jan 26 '23 10:01

Richard Davis


2 Answers

had the same issue, there were 2 problems:

  1. settings.py had a bug.
  2. pytest-django was installed in a different environment.

So ensure you can import settings.py as hoefling recommended, and ensure pytest-django is actually installed in your environment

like image 101
DrSasha Avatar answered May 31 '23 19:05

DrSasha


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.

like image 22
chidimo Avatar answered May 31 '23 17:05

chidimo