I developed a way for test models for django 1.7+.
The basic idea: make your tests application and add tests to INSTALLED_APPS .
Here is 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 (link: splitting the settings file ), namely:
settings/default.py : basic settings filesettings/production.py : for productionsettings/development.py : for developmentsettings/testing.py : for testing.
And in settings/testing.py you can change INSTALLED_APPS :
settings/testing.py :
from default import * DEBUG = True INSTALLED_APPS += ['common', 'common.tests']
And make sure you set the proper label for your test application, namely
common/tests/apps.py
from django.apps import AppConfig class CommonTestsConfig(AppConfig): name = 'common.tests' label = 'common_tests'
common/tests/__init__.py , install the correct AppConfig (ref: Django Applications ).
default_app_config = 'common.tests.apps.CommonTestsConfig'
Then generate the db migration to
python manage.py makemigrations --settings=<your_project_name>.settings.testing tests
Finally, you can run the test with the parameter --settings=<your_project_name>.settings.testing .
If you use py.test, you can even delete the pytest.ini file along with django manage.py .
py.test
[pytest] DJANGO_SETTINGS_MODULE=kungfu.settings.testing
Xiao Hanyu Jan 20 '16 at 13:08 2016-01-20 13:08
source share