Skip to content

Instantly share code, notes, and snippets.

@tganzarolli
Created April 27, 2011 20:15
Show Gist options
  • Save tganzarolli/945093 to your computer and use it in GitHub Desktop.
Save tganzarolli/945093 to your computer and use it in GitHub Desktop.
Generates a custom DjangoTestSuiteRunner that can bypassa the creation of a database instance, if the BYPASS_CREATION entry is set to yes, inside DATABASES dict.
from django.test.simple import DjangoTestSuiteRunner
class ByPassableDBDjangoTestSuiteRunner(DjangoTestSuiteRunner):
def setup_databases(self, **kwargs):
from django.db import connections
old_names = []
mirrors = []
for alias in connections:
connection = connections[alias]
# If the database is a test mirror, redirect it's connection
# instead of creating a test database.
if connection.settings_dict['TEST_MIRROR']:
mirrors.append((alias, connection))
mirror_alias = connection.settings_dict['TEST_MIRROR']
connections._connections[alias] = connections[mirror_alias]
elif connection.settings_dict.get('BYPASS_CREATION','no') == 'no':
old_names.append((connection, connection.settings_dict['NAME']))
connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
return old_names, mirrors
@nicbou
Copy link

nicbou commented Jun 28, 2014

For Django 1.6+, you can use DiscoverRunner instead of DjangoTestSuiteRunner, and it won't break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment