I do have the following project structure
base
__init.py
settings
__init__.py
settings.py
tests
pytest.ini
test_module.py
My pytest.ini
looks like this:
[pytest]
#DJANGO_SETTINGS_MODULE =base.settings.settings
My test_module.py
looks like this:
def test_django():
from base.settings import settings as base_settings
from django.conf import settings as django_settings
assert 3==5
When I now run:
py.test
it will run the imports without issue and will raise an error at assert 3==5
(as expected). This tells me that base is on sys.path
and that base.settings.settings
can be imported.
Now I change test_module.py
to:
def test_django():
from base.settings import settings as base_settings
from django.conf import settings as django_settings
print django_settings.xxx
assert 3==5
When I now run:
py.test --ds=base.settings.settings
I get the error:
ERROR: Could not import settings 'base.settings.settings' (Is it on sys.path?): No module named base.settings.settings.
The same effect when I do not set the settings via command line, but via the pytest.ini
file (by uncommenting the line).
It looks like I miss something here???
There are a couple of different ways Django settings can be provided for the tests. Running the tests with DJANGO_SETTINGS_MODULE defined will find the Django settings the same way Django does by default. The DJANGO_SETTINGS_MODULE option in the configuration file - pytest.ini, or other file that Pytest finds such as tox.ini
When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE. The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.
The Django defaults are sufficiently tame that you can safely use them. Be aware that if you do pass in a new default module, it entirely replaces the Django defaults, so you must specify a value for every possible setting that might be used in that code you are importing. Check in django.conf.settings.global_settings for the full list.
To do so configure the settings class using an environment variable, the --dc flag, or pytest.ini option DJANGO_CONFIGURATION. In case there is no DJANGO_SETTINGS_MODULE, the settings object can be created by calling django.conf.settings.configure ().
Because django.conf.settings is lazy it will attempt to import settings module only when you try to access it. That's why your test doesn't fail when you simply import settings object.
Your problem is already discussed here: https://github.com/pelme/pytest_django/issues/23
This is an issue with pytest and not with pytest-django itself. Pytest for some reason removes current directory from sys.path. It should be easy to work around it.
Solution 1:
PYTHONPATH=`pwd` py.test
Solution 2:
add this to your conftest.py (I assumed that conftest.py is in the same directory your apps are):
import os
import sys
sys.path.append(os.path.dirname(__file__))
Solution 3 (if you're using virtualenv wrapper):
When you start a new project just add project's root directory to virtualenv's PYTHONPATH by executing this line in your project's directory:
add2virtualenv .
From the docs, it seems that base
is already in your path - so maybe you want to be using
py.test --ds=settings.settings
Why are there 3 "t" 's in your settings? it should be settings as opposed to setttings
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