Skip to content

Instantly share code, notes, and snippets.

@alexche8
Last active November 21, 2017 11:02
Show Gist options
  • Save alexche8/4e3a5adaa27b0b14f1bc591c692906f0 to your computer and use it in GitHub Desktop.
Save alexche8/4e3a5adaa27b0b14f1bc591c692906f0 to your computer and use it in GitHub Desktop.
Django: Run tests on production/local database. Keeps database structure and data after.
python manage.py test--settings=path.to.test_settings --keepdb
from django.test.testcases import TransactionTestCase
from django.test.runner import DiscoverRunner
class CustomTestRunner(DiscoverRunner):
def setup_databases(self, **kwargs):
pass
def teardown_databases(self, old_config, **kwargs):
pass
def teardown_test_environment(self, **kwargs):
pass
class TestSearch(TransactionTestCase):
def test_body(self):
# your tests
pass
def tearDown(self):
pass
def _post_teardown(self):
# don't remove data after test
pass
DATABASES = {
'default': {
'ENGINE': 'engine',
'NAME': 'immortal_db',
'USER': 'user',
'PASSWORD': 'user',
'TEST': {
'NAME': 'immortal_db'
}
}
}
TEST_RUNNER = 'path.to.class.CustomTestRunner'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment