Run Unittest to Django's main database

I am looking for a way to run a full celery installation during the django tests asked in this other SO question

After thinking about this, I think I can agree to run unittest (this is rather an integration test), in which I run a script test against the main Django (development) database. Is there a way to write unittests, run them using Nose, and do it against the main database? I suppose it would be to say that Nose (or something else) supports django settings.

I looked through django-nose , but could not find a way to tell it to use the main database, not the test one.

+2
source share
2 answers

I don't know about the nose, but here's how to work against the existing db with django modules (1.6).

 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? 

In earlier versions of django, it will be slightly different. In addition, you may need to override _fixture_setup and _fixture_teardown in your test cases.

The above code will connect to the existing database, but since each test is wrapped in a transaction, the changes will not be available to other connections (for example, a celery employee). The easiest way to disable transactions is to subclass from unittest.TestCase instead of django.test.TestCase .

+4
source

Have you seen django-nose ? It seems like this would be the right tool for the job.

0
source

All Articles