Setting Up Fake Django Application for Testing with Pytest¶
Sometimes we need to test our code in a context of a Django application. Usually this code implements generic components, e.g. custom Django fields. We could always test our components in the context of our Django application, but this breaks the requirement for test isolation. To overcome this problem we could create a Django application which is intended for testing the specific components.
Create The Application¶
$ cd tests
$ django-admin startapp fake_app
Modify the Application name¶
Because django-admin doesn’t support applications in sub-packages, we need to change the name, assigned to our application in the application config:
1from django.apps import AppConfig
2
3class FakeAppConfig(AppConfig):
4 default_auto_field = 'django.db.models.BigAutoField'
5 name = 'tests.fake_app'
Register the Application in Django Settings¶
To isolate the test harness from production code, we register the fake application only in the Django settings module used for testing:
1from .settings import *
2
3INSTALLED_APPS.extend([
4 "tests.fake_app",
5])
Create Migrations¶
Do not forget that when creating migrations you need to specify the testing Django settings module:
$ DJANGO_SETTINGS_MODULE=iris.settings_test python iris/manage.py makemigrations