You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Combining the blog post and gist above gave the following which is working! Thanks to Scot Hacker and the gist author:
fromproject.local_settingsimport*fromdjango.test.runnerimportDiscoverRunnerclassDisableMigrations(object):
def__contains__(self, item):
returnTruedef__getitem__(self, item):
return"notmigrations"classUnManagedModelTestRunner(DiscoverRunner):
''' Test runner that automatically makes all unmanaged models in your Django project managed for the duration of the test run. Many thanks to the Caktus Group: http://bit.ly/1N8TcHW '''defsetup_test_environment(self, *args, **kwargs):
fromdjango.db.models.loadingimportget_modelsself.unmanaged_models= [mforminget_models() ifnotm._meta.managed]
forminself.unmanaged_models:
m._meta.managed=Truesuper(UnManagedModelTestRunner, self).setup_test_environment(*args, **kwargs)
defteardown_test_environment(self, *args, **kwargs):
super(UnManagedModelTestRunner, self).teardown_test_environment(*args, **kwargs)
# reset unmanaged modelsforminself.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 postgres rather than the default, override# some of the global db settings, only to be in effect when "test" is present# in the command line arguments:if'test'insys.argvor'test_coverage'insys.argv: # Covers regular testing and django-coverageDATABASES['default']['ENGINE'] ='django.db.backends.postgresql_psycopg2'DATABASES['default']['HOST'] ='127.0.0.1'DATABASES['default']['USER'] ='username'DATABASES['default']['PASSWORD'] ='secret'DATABASES['tmi']['ENGINE'] ='django.db.backends.postgresql_psycopg2'DATABASES['tmi']['HOST'] ='127.0.0.1'DATABASES['tmi']['USER'] ='username'DATABASES['tmi']['PASSWORD'] ='secret'# The 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.DATABASE_ROUTERS= []
# 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 aboveTEST_RUNNER='project.test_settings.UnManagedModelTestRunner'