This may be a simple question, but I can't wrap my head around it.
Given I make a Django plugin app, lets name it foo, which adds some database features, that other apps can use (e.g. models that must be inherited), and I want to add tests for that app.
Structure:
foo/
apps.py
models.py
tests/
test_foo_basic.py
test_bar.py
...
setup.py
README.md
...
The project is no full Django application, as it is an plugin app for others. So I need to create a fake Django project within the testing directory, with at least one app, which defines some models that inherit from foo.models.<some_model> and test it, right?
Is that the only way? It seems a but much for me.
If so, it's ok for me to give me the answer "it is so". And I'll accept ;-) If not, please tell me I am complicated and show me something easier, a pytest plugin etc.? For testing, I need a
CodePudding user response:
I found out myself, after much struggle.
According to pytest-django docs, you can use pytest_configure() in conftest.py to set up a "pseudo" Django environment for such cases:
# conftest.py
import django
from django.conf import settings
def pytest_configure():
settings.configure(
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
},
INSTALLED_APPS=[
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
"your.app"
],
)
django.setup()
This way, you need no pytest.ini, no DJANGO_SETTINGS_MODULE env, and the best is: ** You need no complete dummy Django project`
It doesn't help if you need to test project features like module loading, etc. But for simple unit tests, this works perfectly.
