[Django]-Run Unittest On Main Django Database

5πŸ‘

βœ…

I don’t know about nose, but here is how to run against and existing db with django (1.6) unit tests.

from django.test.runner import DiscoverRunner
from django.db import transaction

class ExistingDBTestRunner(DiscoverRunner):

    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        self.setup_test_environment()
        suite = self.build_suite(test_labels, extra_tests)
        #old_config = self.setup_databases()
        result = self.run_suite(suite)
        #self.teardown_databases(old_config)
        self.teardown_test_environment()
        return self.suite_result(suite, result)

Then in settings.py

if 'test' in sys.argv:
     TEST_RUNNER = '<?>.ExistingDBTestRunner'
     # alternative db settings?

It will be a little different in older versions of django. Also, you may need to override _fixture_setup and _fixture_teardown in your test cases to pass.

The above code will connect to a preexisting database but since each test is wrapped in a transaction the changes won’t be available to other connections (like the celery worker). The easiest way to disable transactions is to subclass from unittest.TestCase instead of django.test.TestCase.

πŸ‘€joshua

0πŸ‘

Have you had a look at django-nose? It seems like it would be the right tool for the job.

πŸ‘€David

Leave a comment