I've figured out a way for test-only models for django 1.7+.
The basic idea is, make your tests
an app, and add your tests
to INSTALLED_APPS
.
Here's an example:
$ ls common
__init__.py admin.py apps.py fixtures models.py pagination.py tests validators.py views.py
$ ls common/tests
__init__.py apps.py models.py serializers.py test_filter.py test_pagination.py test_validators.py views.py
And I have different settings
for different purposes(ref: splitting up the settings file), namely:
settings/default.py
: base settings file
settings/production.py
: for production
settings/development.py
: for development
settings/testing.py
: for testing.
And in settings/testing.py
, you can modify INSTALLED_APPS
:
settings/testing.py
:
from default import *
DEBUG = True
INSTALLED_APPS += ['common', 'common.tests']
And make sure that you have set a proper label for your tests app, namely,
common/tests/apps.py
from django.apps import AppConfig
class CommonTestsConfig(AppConfig):
name = 'common.tests'
label = 'common_tests'
common/tests/__init__.py
, set up proper AppConfig
(ref: Django Applications).
default_app_config = 'common.tests.apps.CommonTestsConfig'
Then, generate db migration by
python manage.py makemigrations --settings=<your_project_name>.settings.testing tests
Finally, you can run your test with param --settings=<your_project_name>.settings.testing
.
If you use py.test, you can even drop a pytest.ini
file along with django's manage.py
.
py.test
[pytest]
DJANGO_SETTINGS_MODULE=kungfu.settings.testing