Skip to content

Instantly share code, notes, and snippets.

Created March 11, 2020 14:39
What would you like to do?
Django Unit Test Settings For Unmanaged Models
from your_project.settings import *
from django.test.runner import DiscoverRunner
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
class UnManagedModelTestRunner(DiscoverRunner):
Test runner that automatically makes all unmanaged models in your Django
project managed for the duration of the test run.
def setup_test_environment(self, *args, **kwargs):
from django.apps import apps
self.unmanaged_models = [
m for m in apps.get_models() if not m._meta.managed
for m in self.unmanaged_models:
m._meta.managed = True
super(UnManagedModelTestRunner, self).setup_test_environment(
*args, **kwargs
def teardown_test_environment(self, *args, **kwargs):
super(UnManagedModelTestRunner, self).teardown_test_environment(
*args, **kwargs
# reset unmanaged models
for m in self.unmanaged_models:
m._meta.managed = False
# Since we can't create a test db on the read-only host, and we
# want our test dbs created with sqlite rather than the default
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "your_project.sqlite3"),
"TEST": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "your_project.sqlite3"),
AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",)
# Custom routers we're using to route certain ORM queries
# to the remote host conflict with our overridden db settings.
# Set DATABASE_ROUTERS to an empty list to return to the defaults
# during the test run.
# Skip the migrations by setting "MIGRATION_MODULES"
# to the DisableMigrations class defined above
MIGRATION_MODULES = DisableMigrations()
# Set Django's test runner to the custom class defined above
TEST_RUNNER = "your_project.unit-test-settings.UnManagedModelTestRunner"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment