[Django]-How to create a test-only Django model in general (and with pytest-django specifically)

3๐Ÿ‘

โœ…

So I ended up solving my own issue. As @hoefling mentioned, the best way is to create a separate settings file that extends your normal settings file and specify that as your pytest settings file.

// pytest.ini
[pytest]
addopts = --ds=config.settings.test

An important thing to note is that you cannot have your test modules be the same name as already existing apps, as I found out. So the structure I had with backend/tests/richeditable is not allowed no matter what you do. So I prepended each folder with test_ and it works fine. This also solves the issue of having to include app_label in your models.

# testsettings.py
from .settings import *  # noqa

INSTALLED_APPS += [
    'backend.tests.test_projects',
    'backend.tests.test_blog',
    'backend.tests.test_richeditable',
]
backend
โ”œโ”€โ”€ projects
โ”œโ”€โ”€ blog
โ”œโ”€โ”€ richeditable
โ”œโ”€โ”€ tests
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ conftest.py
โ”‚   โ”œโ”€โ”€ test_blog
โ”‚   โ”‚   โ”œโ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ test_projects
โ”‚   โ”‚   โ”œโ”€โ”€ ...
โ”‚   โ””โ”€โ”€ test_richeditable
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ test_model.py
โ”‚   โ”‚   โ”œโ”€โ”€ models.py  
๐Ÿ‘คKalin Kochnev

Leave a comment