Using mock to patch a celery task in Django unit tests

The issue that you are having is unrelated to the fact that this is a Celery task. You just happen to be patching the wrong thing. 😉

Specifically, you need to find out which view or other file is importing “mytask” and patch it over there, so the relevant line would look like this:

with patch('myapp.myview.mytask.delay') as mock_task:

There is some more flavor to this here:

http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

Leave a Comment

File not found.