'How to disable celery tasks while testing on django

Im trying to test some functions that include a call for celery tasks. the tasks involves a call to 3rd party websites and i need to avoid it during tests.

Any idea how to disable all celery tasks during tests?



Solution 1:[1]

Usually the "good method" imply doing Mocks. https://docs.python.org/3/library/unittest.mock.html

So you'll return example response from the site.

You can check on class declaration debug state and if True replace the class by the corresponding Mock

Solution 2:[2]

You can patch the Task delay function

   patch("celery.app.task.Task.delay", return_value=1)
   patch("celery.app.task.Task.apply_async", return_value=1)

If you are using pytest, you can add this in conftest.py:

@pytest.fixture(autouse=True)
def mock_celery_delay(mocker):
    mocker.patch("celery.app.task.Task.delay", return_value=1)
    mocker.patch("celery.app.task.Task.apply_async", return_value=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jTiKey