Unit testing with django-celery?

前端 未结 6 1604
傲寒
傲寒 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

    This is what I did

    Inside myapp.tasks.py I have:

    from celery import shared_task
    
    @shared_task()
    def add(a, b):
        return a + b
    

    Inside myapp.test_tasks.py I have:

    from django.test import TestCase, override_settings
    from myapp.tasks import add
    
    
    class TasksTestCase(TestCase):
    
        def setUp(self):
            ...
    
        @override_settings(CELERY_TASK_ALWAYS_EAGER=True,CELERY_TASK_EAGER_PROPOGATES=True)
        def test_create_sections(self):
            result= add.delay(1,2)
            assert result.successful() == True
            assert result.get() == 3
    

提交回复
热议问题