#7835: Provide the ability for model definitions that are only availably during
testing
-------------------------------------+------------------------------------
Reporter: Russell Keith-Magee | Owner: nobody
Type: New feature | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: feature test models | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Simon Charette):
* cc: Simon Charette (added)
Comment:
While working on a workaround for this I came up with a non-invasive
solution that might be acceptable to resolving the ticket.
The idea is similar to Ashley's solution but is more explicit as it
require a function call in the `app/tests/__init__.py` module. It does
however isolate each app into their own app, which prevent name
collisions, and doesn't require the `app_label = 'test'` assignment on
each test model.
The solutions boils down to this function
{{{#!python
def setup_test_app(package, label=None):
"""
Setup a Django test app for the provided package to allow test models
tables to be created if the containing app has migrations.
This function should be called from app.tests __init__ module and pass
along __package__.
"""
app_config = AppConfig.create(package)
app_config.apps = apps
if label is None:
containing_app_config = apps.get_containing_app_config(package)
label = f'{containing_app_config.label}_tests'
if label in apps.app_configs:
raise ValueError(f"There's already an app registered with the
'{label}' label.')
app_config.label = label
apps.app_configs[app_config.label] = app_config
app_config.import_models()
apps.clear_cache()
}}}
Which when called from `app/tests/__init__.py` as
`setup_test_app(__package__)` will create an `app_tests` appconfig entry
and auto-discover the models automatically. Since the `*.tests` modules
should only be loaded on test discovery the app and its models will only
be available during tests. Keep in mind that if your test models reference
models from an application with migrations you'll also need to manually
create migrations for these tests models but once that's done you should
be good to go.
It does feel less magic and convention based than Ashley's solution as it
prevents conflicts between models and allows multiple test apps per app
from any test package structure. Thoughts?
--
Ticket URL: <https://code.djangoproject.com/ticket/7835#comment:46>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.80f33c734424419e1d5353aa10777b7b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.