Unit testing with django-celery?

前端 未结 6 1586
傲寒
傲寒 2020-12-22 16:35

I am trying to come up with a testing methodology for our django-celery project. I have read the notes in the documentation, but it didn\'t give me a good idea of what to a

6条回答
  •  庸人自扰
    2020-12-22 17:11

    I like to use the override_settings decorator on tests which need celery results to complete.

    from django.test import TestCase
    from django.test.utils import override_settings
    from myapp.tasks import mytask
    
    class AddTestCase(TestCase):
    
        @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                           CELERY_ALWAYS_EAGER=True,
                           BROKER_BACKEND='memory')
        def test_mytask(self):
            result = mytask.delay()
            self.assertTrue(result.successful())
    

    If you want to apply this to all tests you can use the celery test runner as described at http://docs.celeryproject.org/en/2.5/django/unit-testing.html which basically sets these same settings except (BROKER_BACKEND = 'memory').

    In settings:

    TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
    

    Look at the source for CeleryTestSuiteRunner and it's pretty clear what's happening.

提交回复
热议问题