While trying to execute unit tests inside the LMS shell using pytest, I’m facing configuration issues in a customized Open edX Devstack setup.
The traceback shows errors related to pytest_load_initial_conftests and Django settings misconfiguration, particularly with settings.DATABASES.
It seems like pytest-django is unable to detect the right settings module. I’m unsure which Django settings file or environment variable (DJANGO_SETTINGS_MODULE) needs modification to make tests run correctly in this custom environment.
Has anyone faced this issue while running tests in a modified Open edX setup?
How can I properly configure pytest to recognize the right settings module?
In many Devstack setups, pytest-django cannot automatically detect the LMS settings, especially if you’ve customized the environment or directory structure.
You can fix it by explicitly setting the Django settings module before invoking pytest:
export DJANGO_SETTINGS_MODULE=lms.envs.test
pytest lms/
Or, for the CMS:
export DJANGO_SETTINGS_MODULE=cms.envs.test
pytest cms/
Why it works:
The error usually appears because pytest tries to load Django before knowing which settings file to use.
Explicitly setting the environment variable ensures pytest-django uses the correct configuration for database, caches, and middleware.
If you’re inside Devstack, the Open edX project provides helper commands that automatically load the right settings.
Instead of directly calling pytest, use the management command:
./manage.py lms test path.to.your.test --settings=lms.envs.test
or within the container:
make lms-shell
pytest --ds=lms.envs.test
The provided management commands are preconfigured for the LMS or CMS and include environment setup like SERVICE_VARIANT, DJANGO_SETTINGS_MODULE, and EDXAPP_TEST_MONGO_PORT.
Using them bypasses the initialization mismatch that triggers the pytest_load_initial_conftests error.
If you’re running tests frequently, you can avoid repeatedly exporting environment variables by adding a pytest.ini file at the project root:
[pytest]
DJANGO_SETTINGS_MODULE = lms.envs.test
python_paths = .
addopts = --reuse-db
pytest-django automatically picks up configuration from pytest.ini.
This ensures consistent test execution even if you switch between containers or modify the Devstack structure.
It also prevents settings.DATABASES misconfigurations caused by missing environment variables.