質問

I'm looking for a way to run a full celery setup during django tests, asked in this other SO question

After thinking about it, I think I could settle for running a unittest (it's more of an integration test) in which I run the test script against the main Django (development) database. Is there a way to write unittests, run them with Nose and do so against the main database? I imagine it would be a matter of telling Nose (or whatever other framework) about the django settings.

I've looked at django-nose but wasn't able to find a way to tell it to use the main DB and not a test one.

役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top