[Django]-Django Celery how to ignore task?

2👍

To expand on Kevin Stone’s answer, you can mock the Celery part of the test using the patch decorator and MagicMock like this:

Test Code

from unittest.mock import MagicMock, patch

@patch('reference.to.your.long_task', new=MagicMock())
class TestsFunctional(TestCase):

  def test_ignore_task(self):
      response = my_method()
      self.assertEqual(201, response) 

Application Code

def my_method():
  from celery import chain
  chain(tasks.long_task.s(), tasks.another_task.s()).apply_async()
  return 201

@task(default_retry_delay=10, max_retries=None)
def long_task():
  # Long running process

(YMMV)

👤mmla

1👍

I wrote a decorator that I add to functions that I want to skip during unit tests.

https://gist.github.com/kevinastone/7295567

You’re other option would be to use mock and mock out the task.

Leave a comment